1 | /* testscrolledge.c |
2 | * |
3 | * Copyright (C) 2014 Matthias Clasen <mclasen@redhat.com> |
4 | * |
5 | * This program is free software: you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation, either version 3 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | */ |
18 | |
19 | #include <gtk/gtk.h> |
20 | |
21 | static guint add_rows_id = 0; |
22 | |
23 | static void |
24 | populate_list (GtkListBox *list) |
25 | { |
26 | int i; |
27 | char *text; |
28 | GtkWidget *row, *label; |
29 | int n; |
30 | GtkWidget *child; |
31 | |
32 | for (child = gtk_widget_get_first_child (GTK_WIDGET (list)), n = 0; |
33 | child != NULL; |
34 | child = gtk_widget_get_next_sibling (widget: child), n++) ; |
35 | |
36 | for (i = 1; i <= 50; i++) |
37 | { |
38 | row = gtk_list_box_row_new (); |
39 | text = g_strdup_printf (format: "List row %d" , i + n); |
40 | label = gtk_label_new (str: text); |
41 | g_free (mem: text); |
42 | |
43 | gtk_widget_set_margin_start (widget: label, margin: 10); |
44 | gtk_widget_set_margin_end (widget: label, margin: 10); |
45 | gtk_widget_set_margin_top (widget: label, margin: 10); |
46 | gtk_widget_set_margin_bottom (widget: label, margin: 10); |
47 | gtk_widget_set_halign (widget: label, align: GTK_ALIGN_START); |
48 | gtk_list_box_row_set_child (GTK_LIST_BOX_ROW (row), child: label); |
49 | gtk_list_box_insert (GTK_LIST_BOX (list), child: row, position: -1); |
50 | } |
51 | } |
52 | |
53 | static GtkWidget *; |
54 | static GtkWidget *spinner; |
55 | |
56 | static gboolean |
57 | add_rows (gpointer data) |
58 | { |
59 | GtkListBox *list = data; |
60 | |
61 | gtk_widget_hide (widget: popup); |
62 | gtk_spinner_stop (GTK_SPINNER (spinner)); |
63 | |
64 | populate_list (list); |
65 | add_rows_id = 0; |
66 | |
67 | return G_SOURCE_REMOVE; |
68 | } |
69 | |
70 | static void |
71 | edge_overshot (GtkScrolledWindow *sw, |
72 | GtkPositionType pos, |
73 | GtkListBox *list) |
74 | { |
75 | if (pos == GTK_POS_BOTTOM) |
76 | { |
77 | gtk_spinner_start (GTK_SPINNER (spinner)); |
78 | gtk_widget_show (widget: popup); |
79 | |
80 | if (add_rows_id == 0) |
81 | add_rows_id = g_timeout_add (interval: 2000, function: add_rows, data: list); |
82 | } |
83 | } |
84 | |
85 | static void |
86 | edge_reached (GtkScrolledWindow *sw, |
87 | GtkPositionType pos, |
88 | GtkListBox *list) |
89 | { |
90 | g_print (format: "Reached the edge at pos %d!\n" , pos); |
91 | } |
92 | |
93 | int |
94 | main (int argc, char *argv[]) |
95 | { |
96 | GtkWidget *win; |
97 | GtkWidget *sw; |
98 | GtkWidget *list; |
99 | GtkWidget *overlay; |
100 | GtkWidget *label; |
101 | |
102 | gtk_init (); |
103 | |
104 | win = gtk_window_new (); |
105 | gtk_window_set_default_size (GTK_WINDOW (win), width: 600, height: 400); |
106 | |
107 | overlay = gtk_overlay_new (); |
108 | popup = gtk_box_new (orientation: GTK_ORIENTATION_HORIZONTAL, spacing: 10); |
109 | gtk_widget_set_halign (widget: popup, align: GTK_ALIGN_CENTER); |
110 | gtk_widget_set_valign (widget: popup, align: GTK_ALIGN_END); |
111 | gtk_widget_set_margin_start (widget: popup, margin: 40); |
112 | gtk_widget_set_margin_end (widget: popup, margin: 40); |
113 | gtk_widget_set_margin_top (widget: popup, margin: 40); |
114 | gtk_widget_set_margin_bottom (widget: popup, margin: 40); |
115 | label = gtk_label_new (str: "Getting more rows..." ); |
116 | spinner = gtk_spinner_new (); |
117 | gtk_box_append (GTK_BOX (popup), child: label); |
118 | gtk_box_append (GTK_BOX (popup), child: spinner); |
119 | |
120 | gtk_overlay_add_overlay (GTK_OVERLAY (overlay), widget: popup); |
121 | gtk_widget_hide (widget: popup); |
122 | |
123 | sw = gtk_scrolled_window_new (); |
124 | gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), hscrollbar_policy: GTK_POLICY_NEVER, vscrollbar_policy: GTK_POLICY_AUTOMATIC); |
125 | list = gtk_list_box_new (); |
126 | gtk_list_box_set_selection_mode (GTK_LIST_BOX (list), mode: GTK_SELECTION_NONE); |
127 | |
128 | gtk_window_set_child (GTK_WINDOW (win), child: overlay); |
129 | gtk_overlay_set_child (GTK_OVERLAY (overlay), child: sw); |
130 | gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), child: list); |
131 | populate_list (GTK_LIST_BOX (list)); |
132 | |
133 | g_signal_connect (sw, "edge-overshot" , G_CALLBACK (edge_overshot), list); |
134 | g_signal_connect (sw, "edge-reached" , G_CALLBACK (edge_reached), list); |
135 | |
136 | gtk_widget_show (widget: win); |
137 | |
138 | while (TRUE) |
139 | g_main_context_iteration (NULL, TRUE); |
140 | |
141 | return 0; |
142 | } |
143 | |