]> Pileus Git - ~andy/gtk/blob - gtk/tests/grid.c
845e51acf9a43e04ac353927c27d1b69df0e7e89
[~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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <gtk/gtk.h>
21
22 /* test that attach_next_to picks the places
23  * we expect it to pick, when there is any choice
24  */
25 static void
26 test_attach (void)
27 {
28   GtkGrid *g;
29   GtkWidget *child, *sibling, *z, *A, *B;
30   gint left, top, width, height;
31
32   g = (GtkGrid *)gtk_grid_new ();
33
34   child = gtk_label_new ("a");
35   gtk_grid_attach_next_to (g, child, NULL, GTK_POS_LEFT, 1, 1);
36   gtk_container_child_get (GTK_CONTAINER (g), child,
37                            "left-attach", &left,
38                            "top-attach", &top,
39                            "width", &width,
40                            "height", &height,
41                            NULL);
42   g_assert_cmpint (left,   ==, -1);
43   g_assert_cmpint (top,    ==, 0);
44   g_assert_cmpint (width,  ==, 1);
45   g_assert_cmpint (height, ==, 1);
46
47   sibling = child;
48   child = gtk_label_new ("b");
49   gtk_grid_attach_next_to (g, child, sibling, GTK_POS_RIGHT, 2, 2);
50   gtk_container_child_get (GTK_CONTAINER (g), child,
51                            "left-attach", &left,
52                            "top-attach", &top,
53                            "width", &width,
54                            "height", &height,
55                            NULL);
56   g_assert_cmpint (left,   ==, 0);
57   g_assert_cmpint (top,    ==, 0);
58   g_assert_cmpint (width,  ==, 2);
59   g_assert_cmpint (height, ==, 2);
60
61   /* this one should just be ignored */
62   z = gtk_label_new ("z");
63   gtk_grid_attach (g, z, 4, 4, 1, 1);
64
65   child = gtk_label_new ("c");
66   gtk_grid_attach_next_to (g, child, sibling, GTK_POS_BOTTOM, 3, 1);
67   gtk_container_child_get (GTK_CONTAINER (g), child,
68                            "left-attach", &left,
69                            "top-attach", &top,
70                            "width", &width,
71                            "height", &height,
72                            NULL);
73   g_assert_cmpint (left,   ==, -1);
74   g_assert_cmpint (top,    ==, 1);
75   g_assert_cmpint (width,  ==, 3);
76   g_assert_cmpint (height, ==, 1);
77
78   child = gtk_label_new ("u");
79   gtk_grid_attach_next_to (g, child, z, GTK_POS_LEFT, 2, 1);
80   gtk_container_child_get (GTK_CONTAINER (g), child,
81                            "left-attach", &left,
82                            "top-attach", &top,
83                            "width", &width,
84                            "height", &height,
85                            NULL);
86   g_assert_cmpint (left,   ==, 2);
87   g_assert_cmpint (top,    ==, 4);
88   g_assert_cmpint (width,  ==, 2);
89   g_assert_cmpint (height, ==, 1);
90
91   child = gtk_label_new ("v");
92   gtk_grid_attach_next_to (g, child, z, GTK_POS_RIGHT, 2, 1);
93   gtk_container_child_get (GTK_CONTAINER (g), child,
94                            "left-attach", &left,
95                            "top-attach", &top,
96                            "width", &width,
97                            "height", &height,
98                            NULL);
99   g_assert_cmpint (left,   ==, 5);
100   g_assert_cmpint (top,    ==, 4);
101   g_assert_cmpint (width,  ==, 2);
102   g_assert_cmpint (height, ==, 1);
103
104   child = gtk_label_new ("x");
105   gtk_grid_attach_next_to (g, child, z, GTK_POS_TOP, 1, 2);
106   gtk_container_child_get (GTK_CONTAINER (g), child,
107                            "left-attach", &left,
108                            "top-attach", &top,
109                            "width", &width,
110                            "height", &height,
111                            NULL);
112   g_assert_cmpint (left,   ==, 4);
113   g_assert_cmpint (top,    ==, 2);
114   g_assert_cmpint (width,  ==, 1);
115   g_assert_cmpint (height, ==, 2);
116
117   child = gtk_label_new ("x");
118   gtk_grid_attach_next_to (g, child, z, GTK_POS_TOP, 1, 2);
119   gtk_container_child_get (GTK_CONTAINER (g), child,
120                            "left-attach", &left,
121                            "top-attach", &top,
122                            "width", &width,
123                            "height", &height,
124                            NULL);
125   g_assert_cmpint (left,   ==, 4);
126   g_assert_cmpint (top,    ==, 2);
127   g_assert_cmpint (width,  ==, 1);
128   g_assert_cmpint (height, ==, 2);
129
130   child = gtk_label_new ("y");
131   gtk_grid_attach_next_to (g, child, z, GTK_POS_BOTTOM, 1, 2);
132   gtk_container_child_get (GTK_CONTAINER (g), child,
133                            "left-attach", &left,
134                            "top-attach", &top,
135                            "width", &width,
136                            "height", &height,
137                            NULL);
138   g_assert_cmpint (left,   ==, 4);
139   g_assert_cmpint (top,    ==, 5);
140   g_assert_cmpint (width,  ==, 1);
141   g_assert_cmpint (height, ==, 2);
142
143   A = gtk_label_new ("A");
144   gtk_grid_attach (g, A, 10, 10, 1, 1);
145   B = gtk_label_new ("B");
146   gtk_grid_attach (g, B, 10, 12, 1, 1);
147
148   child  = gtk_label_new ("D");
149   gtk_grid_attach_next_to (g, child, A, GTK_POS_RIGHT, 1, 3);
150   gtk_container_child_get (GTK_CONTAINER (g), child,
151                            "left-attach", &left,
152                            "top-attach", &top,
153                            "width", &width,
154                            "height", &height,
155                            NULL);
156   g_assert_cmpint (left,   ==, 11);
157   g_assert_cmpint (top,    ==, 10);
158   g_assert_cmpint (width,  ==,  1);
159   g_assert_cmpint (height, ==,  3);
160 }
161
162 static void
163 test_add (void)
164 {
165   GtkGrid *g;
166   GtkWidget *child;
167   gint left, top, width, height;
168
169   g = (GtkGrid *)gtk_grid_new ();
170
171   gtk_orientable_set_orientation (GTK_ORIENTABLE (g), GTK_ORIENTATION_HORIZONTAL);
172
173   child = gtk_label_new ("a");
174   gtk_container_add (GTK_CONTAINER (g), child);
175   gtk_container_child_get (GTK_CONTAINER (g), child,
176                            "left-attach", &left,
177                            "top-attach", &top,
178                            "width", &width,
179                            "height", &height,
180                            NULL);
181   g_assert_cmpint (left,   ==, 0);
182   g_assert_cmpint (top,    ==, 0);
183   g_assert_cmpint (width,  ==, 1);
184   g_assert_cmpint (height, ==, 1);
185
186   child = gtk_label_new ("b");
187   gtk_container_add (GTK_CONTAINER (g), child);
188   gtk_container_child_get (GTK_CONTAINER (g), child,
189                            "left-attach", &left,
190                            "top-attach", &top,
191                            "width", &width,
192                            "height", &height,
193                            NULL);
194   g_assert_cmpint (left,   ==, 1);
195   g_assert_cmpint (top,    ==, 0);
196   g_assert_cmpint (width,  ==, 1);
197   g_assert_cmpint (height, ==, 1);
198
199   child = gtk_label_new ("c");
200   gtk_container_add (GTK_CONTAINER (g), child);
201   gtk_container_child_get (GTK_CONTAINER (g), child,
202                            "left-attach", &left,
203                            "top-attach", &top,
204                            "width", &width,
205                            "height", &height,
206                            NULL);
207   g_assert_cmpint (left,   ==, 2);
208   g_assert_cmpint (top,    ==, 0);
209   g_assert_cmpint (width,  ==, 1);
210   g_assert_cmpint (height, ==, 1);
211
212   gtk_orientable_set_orientation (GTK_ORIENTABLE (g), GTK_ORIENTATION_VERTICAL);
213
214   child = gtk_label_new ("d");
215   gtk_container_add (GTK_CONTAINER (g), child);
216   gtk_container_child_get (GTK_CONTAINER (g), child,
217                            "left-attach", &left,
218                            "top-attach", &top,
219                            "width", &width,
220                            "height", &height,
221                            NULL);
222   g_assert_cmpint (left,   ==, 0);
223   g_assert_cmpint (top,    ==, 1);
224   g_assert_cmpint (width,  ==, 1);
225   g_assert_cmpint (height, ==, 1);
226 }
227
228 int
229 main (int   argc,
230       char *argv[])
231 {
232   gtk_test_init (&argc, &argv);
233
234   g_test_add_func ("/grid/attach", test_attach);
235   g_test_add_func ("/grid/add", test_add);
236
237   return g_test_run();
238 }