]> Pileus Git - ~andy/gtk/blob - gtk/tests/grid.c
treeview: fix a critical warning
[~andy/gtk] / gtk / tests / grid.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <gtk/gtk.h>
19
20 /* test that attach_next_to picks the places
21  * we expect it to pick, when there is any choice
22  */
23 static void
24 test_attach (void)
25 {
26   GtkGrid *g;
27   GtkWidget *child, *sibling, *z, *A, *B;
28   gint left, top, width, height;
29
30   g = (GtkGrid *)gtk_grid_new ();
31
32   child = gtk_label_new ("a");
33   gtk_grid_attach_next_to (g, child, NULL, GTK_POS_LEFT, 1, 1);
34   gtk_container_child_get (GTK_CONTAINER (g), child,
35                            "left-attach", &left,
36                            "top-attach", &top,
37                            "width", &width,
38                            "height", &height,
39                            NULL);
40   g_assert_cmpint (left,   ==, -1);
41   g_assert_cmpint (top,    ==, 0);
42   g_assert_cmpint (width,  ==, 1);
43   g_assert_cmpint (height, ==, 1);
44
45   sibling = child;
46   child = gtk_label_new ("b");
47   gtk_grid_attach_next_to (g, child, sibling, GTK_POS_RIGHT, 2, 2);
48   gtk_container_child_get (GTK_CONTAINER (g), child,
49                            "left-attach", &left,
50                            "top-attach", &top,
51                            "width", &width,
52                            "height", &height,
53                            NULL);
54   g_assert_cmpint (left,   ==, 0);
55   g_assert_cmpint (top,    ==, 0);
56   g_assert_cmpint (width,  ==, 2);
57   g_assert_cmpint (height, ==, 2);
58
59   /* this one should just be ignored */
60   z = gtk_label_new ("z");
61   gtk_grid_attach (g, z, 4, 4, 1, 1);
62
63   child = gtk_label_new ("c");
64   gtk_grid_attach_next_to (g, child, sibling, GTK_POS_BOTTOM, 3, 1);
65   gtk_container_child_get (GTK_CONTAINER (g), child,
66                            "left-attach", &left,
67                            "top-attach", &top,
68                            "width", &width,
69                            "height", &height,
70                            NULL);
71   g_assert_cmpint (left,   ==, -1);
72   g_assert_cmpint (top,    ==, 1);
73   g_assert_cmpint (width,  ==, 3);
74   g_assert_cmpint (height, ==, 1);
75
76   child = gtk_label_new ("u");
77   gtk_grid_attach_next_to (g, child, z, GTK_POS_LEFT, 2, 1);
78   gtk_container_child_get (GTK_CONTAINER (g), child,
79                            "left-attach", &left,
80                            "top-attach", &top,
81                            "width", &width,
82                            "height", &height,
83                            NULL);
84   g_assert_cmpint (left,   ==, 2);
85   g_assert_cmpint (top,    ==, 4);
86   g_assert_cmpint (width,  ==, 2);
87   g_assert_cmpint (height, ==, 1);
88
89   child = gtk_label_new ("v");
90   gtk_grid_attach_next_to (g, child, z, GTK_POS_RIGHT, 2, 1);
91   gtk_container_child_get (GTK_CONTAINER (g), child,
92                            "left-attach", &left,
93                            "top-attach", &top,
94                            "width", &width,
95                            "height", &height,
96                            NULL);
97   g_assert_cmpint (left,   ==, 5);
98   g_assert_cmpint (top,    ==, 4);
99   g_assert_cmpint (width,  ==, 2);
100   g_assert_cmpint (height, ==, 1);
101
102   child = gtk_label_new ("x");
103   gtk_grid_attach_next_to (g, child, z, GTK_POS_TOP, 1, 2);
104   gtk_container_child_get (GTK_CONTAINER (g), child,
105                            "left-attach", &left,
106                            "top-attach", &top,
107                            "width", &width,
108                            "height", &height,
109                            NULL);
110   g_assert_cmpint (left,   ==, 4);
111   g_assert_cmpint (top,    ==, 2);
112   g_assert_cmpint (width,  ==, 1);
113   g_assert_cmpint (height, ==, 2);
114
115   child = gtk_label_new ("x");
116   gtk_grid_attach_next_to (g, child, z, GTK_POS_TOP, 1, 2);
117   gtk_container_child_get (GTK_CONTAINER (g), child,
118                            "left-attach", &left,
119                            "top-attach", &top,
120                            "width", &width,
121                            "height", &height,
122                            NULL);
123   g_assert_cmpint (left,   ==, 4);
124   g_assert_cmpint (top,    ==, 2);
125   g_assert_cmpint (width,  ==, 1);
126   g_assert_cmpint (height, ==, 2);
127
128   child = gtk_label_new ("y");
129   gtk_grid_attach_next_to (g, child, z, GTK_POS_BOTTOM, 1, 2);
130   gtk_container_child_get (GTK_CONTAINER (g), child,
131                            "left-attach", &left,
132                            "top-attach", &top,
133                            "width", &width,
134                            "height", &height,
135                            NULL);
136   g_assert_cmpint (left,   ==, 4);
137   g_assert_cmpint (top,    ==, 5);
138   g_assert_cmpint (width,  ==, 1);
139   g_assert_cmpint (height, ==, 2);
140
141   A = gtk_label_new ("A");
142   gtk_grid_attach (g, A, 10, 10, 1, 1);
143   B = gtk_label_new ("B");
144   gtk_grid_attach (g, B, 10, 12, 1, 1);
145
146   child  = gtk_label_new ("D");
147   gtk_grid_attach_next_to (g, child, A, GTK_POS_RIGHT, 1, 3);
148   gtk_container_child_get (GTK_CONTAINER (g), child,
149                            "left-attach", &left,
150                            "top-attach", &top,
151                            "width", &width,
152                            "height", &height,
153                            NULL);
154   g_assert_cmpint (left,   ==, 11);
155   g_assert_cmpint (top,    ==, 10);
156   g_assert_cmpint (width,  ==,  1);
157   g_assert_cmpint (height, ==,  3);
158 }
159
160 static void
161 test_add (void)
162 {
163   GtkGrid *g;
164   GtkWidget *child;
165   gint left, top, width, height;
166
167   g = (GtkGrid *)gtk_grid_new ();
168
169   gtk_orientable_set_orientation (GTK_ORIENTABLE (g), GTK_ORIENTATION_HORIZONTAL);
170
171   child = gtk_label_new ("a");
172   gtk_container_add (GTK_CONTAINER (g), child);
173   gtk_container_child_get (GTK_CONTAINER (g), child,
174                            "left-attach", &left,
175                            "top-attach", &top,
176                            "width", &width,
177                            "height", &height,
178                            NULL);
179   g_assert_cmpint (left,   ==, 0);
180   g_assert_cmpint (top,    ==, 0);
181   g_assert_cmpint (width,  ==, 1);
182   g_assert_cmpint (height, ==, 1);
183
184   child = gtk_label_new ("b");
185   gtk_container_add (GTK_CONTAINER (g), child);
186   gtk_container_child_get (GTK_CONTAINER (g), child,
187                            "left-attach", &left,
188                            "top-attach", &top,
189                            "width", &width,
190                            "height", &height,
191                            NULL);
192   g_assert_cmpint (left,   ==, 1);
193   g_assert_cmpint (top,    ==, 0);
194   g_assert_cmpint (width,  ==, 1);
195   g_assert_cmpint (height, ==, 1);
196
197   child = gtk_label_new ("c");
198   gtk_container_add (GTK_CONTAINER (g), child);
199   gtk_container_child_get (GTK_CONTAINER (g), child,
200                            "left-attach", &left,
201                            "top-attach", &top,
202                            "width", &width,
203                            "height", &height,
204                            NULL);
205   g_assert_cmpint (left,   ==, 2);
206   g_assert_cmpint (top,    ==, 0);
207   g_assert_cmpint (width,  ==, 1);
208   g_assert_cmpint (height, ==, 1);
209
210   gtk_orientable_set_orientation (GTK_ORIENTABLE (g), GTK_ORIENTATION_VERTICAL);
211
212   child = gtk_label_new ("d");
213   gtk_container_add (GTK_CONTAINER (g), child);
214   gtk_container_child_get (GTK_CONTAINER (g), child,
215                            "left-attach", &left,
216                            "top-attach", &top,
217                            "width", &width,
218                            "height", &height,
219                            NULL);
220   g_assert_cmpint (left,   ==, 0);
221   g_assert_cmpint (top,    ==, 1);
222   g_assert_cmpint (width,  ==, 1);
223   g_assert_cmpint (height, ==, 1);
224 }
225
226 int
227 main (int   argc,
228       char *argv[])
229 {
230   gtk_test_init (&argc, &argv);
231
232   g_test_add_func ("/grid/attach", test_attach);
233   g_test_add_func ("/grid/add", test_add);
234
235   return g_test_run();
236 }