summaryrefslogtreecommitdiff
path: root/endless/eospagemanager.c
blob: d8b7ea5ae685fa5062211194d539331c22ac0a47 (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
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
/* Copyright 2013 Endless Mobile, Inc. */

#include "config.h"
#include "eospagemanager.h"
#include "eospagemanager-private.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 #EosSplashPageManager 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_uri", "image.jpg",
 *                                    NULL);
 * ]|
 * In Javascript, this has been simplified to use JSON:
 * |[
 * page_manager.add(page, {
 *     name: 'front-page',
 *     background_uri: '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 there are multiple pages still in the page manager, you should never
 * remove the visible-page. Always set a new visible page before removing the
 * current one. A critical warning will be emitted if you remove the visible-
 * page when there are still other pages in the page manager.
 *
 * <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.
 */

#define DEFAULT_BACKGROUND_SIZE "100% 100%"
#define DEFAULT_BACKGROUND_POSITION "0% 0%"

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;
  GtkWidget *left_topbar_widget;
  GtkWidget *center_topbar_widget;
  gchar *background_uri;
  gchar *background_size;
  gchar *background_position;
  gboolean background_repeats;
};

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;
  EosPageManagerTransitionType transition_type;
};

GType
eos_page_manager_transition_type_get_type (void)
{
  static GType etype = 0;
  if (G_UNLIKELY(etype == 0)) {
    static const GEnumValue values[] = {
      { EOS_PAGE_MANAGER_TRANSITION_TYPE_NONE, "EOS_PAGE_MANAGER_TRANSITION_TYPE_NONE", "none" },
      { EOS_PAGE_MANAGER_TRANSITION_TYPE_CROSSFADE, "EOS_PAGE_MANAGER_TRANSITION_TYPE_CROSSFADE", "crossfade" },
      { EOS_PAGE_MANAGER_TRANSITION_TYPE_SLIDE_RIGHT, "EOS_PAGE_MANAGER_TRANSITION_TYPE_SLIDE_RIGHT", "slide_right" },
      { EOS_PAGE_MANAGER_TRANSITION_TYPE_SLIDE_LEFT, "EOS_PAGE_MANAGER_TRANSITION_TYPE_SLIDE_LEFT", "slide_left" },
      { EOS_PAGE_MANAGER_TRANSITION_TYPE_SLIDE_UP, "EOS_PAGE_MANAGER_TRANSITION_TYPE_SLIDE_UP", "slide_up" },
      { EOS_PAGE_MANAGER_TRANSITION_TYPE_SLIDE_DOWN, "EOS_PAGE_MANAGER_TRANSITION_TYPE_SLIDE_DOWN", "slide_down" },
      { 0, NULL, NULL }
    };
    etype = g_enum_register_static (g_intern_static_string ("EosPageManagerTransitionType"), values);
  }
  return etype;
}

enum
{
  PROP_0,
  PROP_VISIBLE_PAGE,
  PROP_VISIBLE_PAGE_NAME,
  PROP_TRANSITION_DURATION,
  PROP_TRANSITION_TYPE,
  NPROPS
};

enum
{
  CHILD_PROP_0,
  CHILD_PROP_NAME,
  CHILD_PROP_PAGE_ACTIONS,
  CHILD_PROP_CUSTOM_TOOLBOX_WIDGET,
  CHILD_PROP_LEFT_TOPBAR_WIDGET,
  CHILD_PROP_CENTER_TOPBAR_WIDGET,
  CHILD_PROP_BACKGROUND_URI,
  CHILD_PROP_BACKGROUND_SIZE,
  CHILD_PROP_BACKGROUND_POSITION,
  CHILD_PROP_BACKGROUND_REPEATS,
  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_free (info->background_uri);
  g_free (info->background_size);
  g_free (info->background_position);
  g_slice_free (EosPageManagerPageInfo, info);
}

static void
top_bars_unref (EosPageManagerPageInfo *info)
{
  g_clear_object (&info->left_topbar_widget);
  g_clear_object (&info->center_topbar_widget);
}

/*
 * 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)
{
  GtkStack *stack = GTK_STACK (self->priv->stack);
  gtk_stack_set_visible_child (stack, info->page);

  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;

    case PROP_TRANSITION_DURATION:
      g_value_set_uint (value, eos_page_manager_get_transition_duration (self));
      break;

    case PROP_TRANSITION_TYPE:
      g_value_set_enum (value, eos_page_manager_get_transition_type (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;

    case PROP_TRANSITION_DURATION:
      eos_page_manager_set_transition_duration (self, g_value_get_uint (value));
      break;

    case PROP_TRANSITION_TYPE:
      eos_page_manager_set_transition_type (self, g_value_get_enum (value));
      break;

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

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

  g_list_foreach (self->priv->page_info, (GFunc)top_bars_unref, NULL);

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

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->background_size = g_strdup (DEFAULT_BACKGROUND_SIZE);
  info->background_position = g_strdup (DEFAULT_BACKGROUND_POSITION);
  info->background_repeats = TRUE;
  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)
    eos_page_manager_set_visible_page (self, new_page);

  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 (self->priv->visible_page_info == info)
    {
      /* If this was the only page */
      if (self->priv->page_info == NULL)
        {
          self->priv->visible_page_info = NULL;
        }
      /* Otherwise set visible page as the first in our list. */
      else
        {
          g_critical ("Removing the currently visible page %p from the page manager.",
                      page);
          EosPageManagerPageInfo *visible_info = g_list_first (self->priv->page_info)->data;
          set_visible_page_from_info (self, visible_info);
        }

    }

  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_BACKGROUND_URI:
      g_value_set_string (value,
                          eos_page_manager_get_page_background_uri (self,
                                                                    child));
      break;

    case CHILD_PROP_BACKGROUND_SIZE:
      g_value_set_string (value,
                          eos_page_manager_get_page_background_size (self,
                                                                     child));
      break;

    case CHILD_PROP_BACKGROUND_POSITION:
      g_value_set_string (value,
                          eos_page_manager_get_page_background_position (self,
                                                                         child));
      break;

    case CHILD_PROP_BACKGROUND_REPEATS:
      g_value_set_boolean (value,
                          eos_page_manager_get_page_background_repeats (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;

    case CHILD_PROP_LEFT_TOPBAR_WIDGET:
      g_value_set_object (value,
                          eos_page_manager_get_page_left_topbar_widget (self,
                                                                        child));
      break;

    case CHILD_PROP_CENTER_TOPBAR_WIDGET:
      g_value_set_object (value,
                          eos_page_manager_get_page_center_topbar_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_BACKGROUND_URI:
      eos_page_manager_set_page_background_uri (self, child,
                                                g_value_get_string (value));
      break;

    case CHILD_PROP_BACKGROUND_SIZE:
      eos_page_manager_set_page_background_size (self, child,
                                                 g_value_get_string (value));
      break;

    case CHILD_PROP_BACKGROUND_POSITION:
      eos_page_manager_set_page_background_position (self, child,
                                                     g_value_get_string (value));
      break;

    case CHILD_PROP_BACKGROUND_REPEATS:
      eos_page_manager_set_page_background_repeats (self, child,
                                                    g_value_get_boolean (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;

    case CHILD_PROP_LEFT_TOPBAR_WIDGET:
      eos_page_manager_set_page_left_topbar_widget (self, child,
                                                    g_value_get_object (value));
      break;

    case CHILD_PROP_CENTER_TOPBAR_WIDGET:
      eos_page_manager_set_page_center_topbar_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->dispose = eos_page_manager_dispose;
  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);

  /**
   * EosPageManager:transition-duration:
   *
   * The time it will take to animate between pages in the page managers, in
   * milliseconds.
   */
  eos_page_manager_props[PROP_TRANSITION_DURATION] =
    g_param_spec_uint ("transition-duration", "Transition duration",
                       "The animation duration, in milliseconds",
                       0, G_MAXUINT,
                       200,
                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT);


  /**
   * EosPageManager:transition-type:
   *
   * The type of animation to use when switching between pages in the page
   * manager. The pages can crossfade from one to the next, or slide in from
   * any direction.
   */
  eos_page_manager_props[PROP_TRANSITION_TYPE] =
    g_param_spec_enum ("transition-type", "Transition type",
                       "The type of animation used to transition",
                       EOS_TYPE_PAGE_MANAGER_TRANSITION_TYPE,
                       EOS_PAGE_MANAGER_TRANSITION_TYPE_NONE,
                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT);

  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);

  /**
   * EosPageManager:left-topbar-widget:
   *
   * The left topbar widget belonging to this page, to be displayed on the
   * left portion of the top bar when the page is displaying. Setting this to
   * %NULL indicates that there should be no left topbar widget.
   */
  eos_page_manager_child_props[CHILD_PROP_LEFT_TOPBAR_WIDGET] =
    g_param_spec_object ("left-topbar-widget", "Left topbar widget",
                         "Left topbar widget displayed left of the topbar",
                         GTK_TYPE_WIDGET,
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  /**
   * EosPageManager:center-topbar-widget:
   *
   * The center topbar widget belonging to this page, to be displayed on the
   * middle portion of the top bar when the page is displaying. Setting this to
   * %NULL indicates that there should be no center topbar widget.
   */
  eos_page_manager_child_props[CHILD_PROP_CENTER_TOPBAR_WIDGET] =
    g_param_spec_object ("center-topbar-widget", "Center topbar widget",
                         "Center topbar widget displayed in the middle of the topbar",
                         GTK_TYPE_WIDGET,
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  /**
   * EosPageManager:background-uri:
   *
   * The URI for the image file for the background of this page. Setting this to
   * %NULL indicates that the window's default background should be used.
   */
  eos_page_manager_child_props[CHILD_PROP_BACKGROUND_URI] =
    g_param_spec_string ("background-uri", "Background URI",
                         "URI for background of the page",
                         NULL,
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  /**
   * EosPageManager:background-size:
   *
   * The size of the page background, given as a css snippet string. This
   * string can be set to any valid css value for the background-size
   * property. See https://developer.mozilla.org/en-US/docs/Web/CSS/background-size
   */
  eos_page_manager_child_props[CHILD_PROP_BACKGROUND_SIZE] =
    g_param_spec_string ("background-size", "Background Size",
                         "Size of background of the page",
                         DEFAULT_BACKGROUND_SIZE,
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  /**
   * EosPageManager:background-position:
   *
   * The position of the page background, given as a css snippet string. This
   * string can be set to any valid css position value. See
   * https://developer.mozilla.org/en-US/docs/Web/CSS/position_value
   */
  eos_page_manager_child_props[CHILD_PROP_BACKGROUND_POSITION] =
    g_param_spec_string ("background-position", "Background Position",
                         "Position of background of the page",
                         DEFAULT_BACKGROUND_POSITION,
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);

  /**
   * EosPageManager:background-repeats:
   *
   * This child property controls whether or not the background of the page
   * will tile when drawn. Defaults to TRUE.
   */
  eos_page_manager_child_props[CHILD_PROP_BACKGROUND_REPEATS] =
    g_param_spec_boolean ("background-repeats", "Background Repeats",
                         "If page background repeats",
                         TRUE,
                         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_stack_new ();
  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_get_page_left_topbar_widget:
 * @self: the page manager
 * @page: the page to be queried
 *
 * Retrieves @page's left topbar widget, if it has one.
 * See #EosPageManager:left-topbar-widget for more information.
 * 
 * Returns: (transfer none): the left topbar #GtkWidget of @page, or %NULL if there is none.
 */
GtkWidget *
eos_page_manager_get_page_left_topbar_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->left_topbar_widget;
}

/**
 * eos_page_manager_set_page_left_topbar_widget:
 * @self: the page manager
 * @page: the page
 * @left_topbar_widget: (allow-none): left topbar widget for @page
 * 
 * Sets the left topbar widget to be displayed for this @page. 
 * See #EosPageManager:left-topbar-widget for more information.
 */
void
eos_page_manager_set_page_left_topbar_widget (EosPageManager *self,
                                              GtkWidget      *page,
                                              GtkWidget      *left_topbar_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 (left_topbar_widget == NULL ||
                    GTK_IS_WIDGET (left_topbar_widget));

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

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

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

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

  gtk_container_child_notify (GTK_CONTAINER (self), page,
                              "left-topbar-widget");
}

/**
 * eos_page_manager_get_page_center_topbar_widget:
 * @self: the page manager
 * @page: the page to be queried
 * 
 * Retrieves @page's center topbar widget, if it has one.
 * See #EosPageManager:center-topbar-widget for more information.
 * 
 * Returns: (transfer none): the center topbar #GtkWidget of @page, or %NULL if there is none.
 */
 GtkWidget *
 eos_page_manager_get_page_center_topbar_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->center_topbar_widget;
}

/**
 * eos_page_manager_set_page_center_topbar_widget:
 * @self: the page manager
 * @page: the page
 * @center_topbar_widget: (allow-none): center topbar widget for @page
 * 
 * Sets the center topbar widget to be displayed for this @page.
 * See #EosPageManager:center-topbar-widget for more information.
 */
void
eos_page_manager_set_page_center_topbar_widget (EosPageManager *self,
                                                GtkWidget      *page,
                                                GtkWidget      *center_topbar_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 (center_topbar_widget == NULL ||
                    GTK_IS_WIDGET (center_topbar_widget));

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

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

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

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

  gtk_container_child_notify (GTK_CONTAINER (self), page,
                              "center-topbar-widget");
}

/**
 * eos_page_manager_get_page_background_uri:
 * @self: the page manager
 * @page: the page to be queried
 *
 * Gets the URI for the background image of @page, which must previously have
 * been added to the page manager.
 * See #EosPageManager:background-uri for more information.
 *
 * Returns: the background of @page, or the %NULL if @page does not have a
 * background.
 */
const gchar *
eos_page_manager_get_page_background_uri (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->background_uri;
}

/**
 * eos_page_manager_set_page_background_uri:
 * @self: the page manager
 * @page: the page to be modified
 * @background: (allow-none): the URI for the background image of this page.
 *
 * Changes the background of @page, which must previously have been added to the
 * page manager.
 * Setting %NULL removes the background, using the window's default background.
 * See #EosPageManager:background-uri for more information.
 */
void
eos_page_manager_set_page_background_uri (EosPageManager *self,
                                          GtkWidget      *page,
                                          const gchar    *background)
{
  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 (g_strcmp0 (info->background_uri, background) == 0)
    return;

  info->background_uri = g_strdup (background);
  gtk_container_child_notify (GTK_CONTAINER (self), page, "background-uri");
}

/**
 * eos_page_manager_get_page_background_size:
 * @self: the page manager
 * @page: the page to be queried
 *
 * Gets the size of the background image of @page, which must previously have
 * been added to the page manager.
 * See #EosPageManager:background-size for more information.
 *
 * Returns: the size of @page, as an EosSizePercentage.
 */
const gchar *
eos_page_manager_get_page_background_size (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->background_size;
}

/**
 * eos_page_manager_set_page_background_size:
 * @self: the page manager
 * @page: the page to be modified
 * @size: the desired size of the background image of this page.
 *
 * Changes the size of the background of @page, which must previously have
 * been added to the page manager. See #EosPageManager:background-size for
 * more information.
 */
void
eos_page_manager_set_page_background_size (EosPageManager *self,
                                          GtkWidget      *page,
                                          const gchar    *size)
{
  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 (g_strcmp0 (info->background_size, size) == 0)
    return;

  info->background_size = g_strdup (size);
  gtk_container_child_notify (GTK_CONTAINER (self), page, "background-size");
}


/**
 * eos_page_manager_get_page_background_position:
 * @self: the page manager
 * @page: the page to be queried
 *
 * Gets the position of the background image of @page, which must previously have
 * been added to the page manager.
 * See #EosPageManager:background-position for more information.
 *
 * Returns: the position of @page, as an EosSizePercentage.
 */
const gchar *
eos_page_manager_get_page_background_position (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->background_position;
}

/**
 * eos_page_manager_set_page_background_position:
 * @self: the page manager
 * @page: the page to be modified
 * @position: the desired position of the background image of this page.
 *
 * Changes the position of the background of @page, which must previously have
 * been added to the page manager. See #EosPageManager:background-position for
 * more information.
 */
void
eos_page_manager_set_page_background_position (EosPageManager *self,
                                              GtkWidget      *page,
                                              const gchar    *position)
{
  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 (g_strcmp0 (info->background_position, position) == 0)
    return;

  info->background_position = g_strdup (position);
  gtk_container_child_notify (GTK_CONTAINER (self), page, "background-position");
}


/**
 * eos_page_manager_get_page_background_repeats:
 * @self: the page manager
 * @page: the page to be queried
 *
 * Gets whether or not the background image of @page will tile when drawn.
 * @page must previously have been added to the page manager. See
 * #EosPageManager:background-repeats for more information.
 *
 * Returns: True if the background of @page will repeat.
 */
gboolean
eos_page_manager_get_page_background_repeats (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->background_repeats;
}

/**
 * eos_page_manager_set_page_background_repeats:
 * @self: the page manager
 * @page: the page to be modified
 * @repeats: True if the background of @page will repeat.
 *
 * Sets whether or not the background image of @page will tile when drawn.
 * @page must previously have been added to the page manager. See
 * #EosPageManager:background-repeats for more information.
 */
void
eos_page_manager_set_page_background_repeats (EosPageManager *self,
                                              GtkWidget      *page,
                                              gboolean        repeats)
{
  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);

  info->background_repeats = repeats;
  gtk_container_child_notify (GTK_CONTAINER (self), page, "background-repeats");
}

/**
 * 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);
}

/**
 * eos_page_manager_get_transition_duration:
 * @self: the page manager
 *
 * Gets the animation duration of page transitions, in milliseconds. See
 * #EosPageManager:transition-duration for more information.
 *
 * Returns: the current transition time of the page manager.
 */
guint
eos_page_manager_get_transition_duration (EosPageManager *self)
{
  g_return_val_if_fail (EOS_IS_PAGE_MANAGER (self), 0);

  return gtk_stack_get_transition_duration (GTK_STACK (self->priv->stack));
}

/**
 * eos_page_manager_set_transition_duration:
 * @self: the page manager
 * @duration: the duration of page transitions, in milliseconds
 *
 * Sets the animation duration of page transitions, in milliseconds. See
 * #EosPageManager:transition-duration for more information.
 */
void
eos_page_manager_set_transition_duration (EosPageManager *self,
                                          guint           duration)
{
  g_return_if_fail (EOS_IS_PAGE_MANAGER (self));

  gtk_stack_set_transition_duration (GTK_STACK (self->priv->stack), duration);
  g_object_notify (G_OBJECT (self), "transition-duration");
}

/**
 * eos_page_manager_get_transition_type:
 * @self: the page manager
 *
 * Gets the animation type of page transitions. See
 * #EosPageManager:transition-type for more information.
 *
 * Returns: the current transition type of the page manager.
 */
EosPageManagerTransitionType
eos_page_manager_get_transition_type (EosPageManager *self)
{
  g_return_val_if_fail (EOS_IS_PAGE_MANAGER (self), EOS_PAGE_MANAGER_TRANSITION_TYPE_NONE);

  return self->priv->transition_type;
}


/**
 * eos_page_manager_set_transition_type:
 * @self: the page manager
 * @transition_type: the type of page transitions
 *
 * Sets the animation type of page transitions. See
 * #EosPageManager:transition-type for more information.
 */
void
eos_page_manager_set_transition_type (EosPageManager                *self,
                                      EosPageManagerTransitionType   transition_type)
{
  g_return_if_fail (EOS_IS_PAGE_MANAGER (self));

  self->priv->transition_type = transition_type;
  GtkStackTransitionType gtk_stack_transition;
  switch (transition_type)
    {
    case EOS_PAGE_MANAGER_TRANSITION_TYPE_NONE:
    case EOS_PAGE_MANAGER_TRANSITION_TYPE_CROSSFADE:
    case EOS_PAGE_MANAGER_TRANSITION_TYPE_SLIDE_RIGHT:
    case EOS_PAGE_MANAGER_TRANSITION_TYPE_SLIDE_LEFT:
    case EOS_PAGE_MANAGER_TRANSITION_TYPE_SLIDE_UP:
    case EOS_PAGE_MANAGER_TRANSITION_TYPE_SLIDE_DOWN:
      gtk_stack_transition = (GtkStackTransitionType)self->priv->transition_type;
      break;
    default:
      gtk_stack_transition = GTK_STACK_TRANSITION_TYPE_NONE;
      break;
    }
  gtk_stack_set_transition_type (GTK_STACK (self->priv->stack),
                                 gtk_stack_transition);
  g_object_notify (G_OBJECT (self), "transition-type");
}

/*
 * eos_page_manager_get_gtk_stack_transition_type:
 * @self: the page manager
 *
 * Gets the internal gtk_stack transition type used to animate the page manager.
 */
GtkStackTransitionType
eos_page_manager_get_gtk_stack_transition_type (EosPageManager *self)
{
  return gtk_stack_get_transition_type (GTK_STACK (self->priv->stack));
}