Debian Codegraph @

demo/pages/carousel/adw-demo-page-carousel.c

raw
1 #include "adw-demo-page-carousel.h"
2
3 #include <glib/gi18n.h>
4
5 struct _AdwDemoPageCarousel
6 {
7 AdwBin parent_instance;
8
9 GtkBox *box;
10 AdwCarousel *carousel;
11 GtkStack *indicators_stack;
12 AdwComboRow *orientation_row;
13 AdwComboRow *indicators_row;
14 };
15
16 G_DEFINE_FINAL_TYPE (AdwDemoPageCarousel, adw_demo_page_carousel, ADW_TYPE_BIN)
17
18 static char *
19 get_orientation_name (AdwEnumListItem *item,
20 gpointer user_data)
21 {
22 switch (adw_enum_list_item_get_value (item)) {
23 case GTK_ORIENTATION_HORIZONTAL:
24 return g_strdup (_("Horizontal"));
25 case GTK_ORIENTATION_VERTICAL:
26 return g_strdup (_("Vertical"));
27 default:
28 return NULL;
29 }
30 }
31
32 static void
33 notify_orientation_cb (AdwDemoPageCarousel *self)
34 {
35 GtkOrientation orientation = adw_combo_row_get_selected (self->orientation_row);
36
37 gtk_orientable_set_orientation (GTK_ORIENTABLE (self->box),
38 1 - orientation);
39 gtk_orientable_set_orientation (GTK_ORIENTABLE (self->carousel),
40 orientation);
41 }
42
43 static char *
44 get_indicators_name (GtkStringObject *value)
45 {
46 const char *style;
47
48 g_assert (GTK_IS_STRING_OBJECT (value));
49
50 style = gtk_string_object_get_string (value);
51
52 if (!g_strcmp0 (style, "dots"))
53 return g_strdup (_("Dots"));
54
55 if (!g_strcmp0 (style, "lines"))
56 return g_strdup (_("Lines"));
57
58 return NULL;
59 }
60
61 static void
62 notify_indicators_cb (AdwDemoPageCarousel *self)
63 {
64 GtkStringObject *obj = adw_combo_row_get_selected_item (self->indicators_row);
65
66 gtk_stack_set_visible_child_name (self->indicators_stack,
67 gtk_string_object_get_string (obj));
68 }
69
70 static void
71 carousel_return_cb (AdwDemoPageCarousel *self)
72 {
73 adw_carousel_scroll_to (self->carousel,
74 adw_carousel_get_nth_page (self->carousel, 0),
75 TRUE);
76 }
77
78 static void
79 adw_demo_page_carousel_class_init (AdwDemoPageCarouselClass *klass)
80 {
81 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
82
83 gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Adwaita1/Demo/ui/pages/carousel/adw-demo-page-carousel.ui");
84 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageCarousel, box);
85 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageCarousel, carousel);
86 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageCarousel, indicators_stack);
87 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageCarousel, orientation_row);
88 gtk_widget_class_bind_template_child (widget_class, AdwDemoPageCarousel, indicators_row);
89 gtk_widget_class_bind_template_callback (widget_class, get_orientation_name);
90 gtk_widget_class_bind_template_callback (widget_class, notify_orientation_cb);
91 gtk_widget_class_bind_template_callback (widget_class, get_indicators_name);
92 gtk_widget_class_bind_template_callback (widget_class, notify_indicators_cb);
93
94 gtk_widget_class_install_action (widget_class, "carousel.return", NULL, (GtkWidgetActionActivateFunc) carousel_return_cb);
95 }
96
97 static void
98 adw_demo_page_carousel_init (AdwDemoPageCarousel *self)
99 {
100 gtk_widget_init_template (GTK_WIDGET (self));
101 }