]> Pileus Git - ~andy/gtk/blob - tests/testverticalcells.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / testverticalcells.c
1 /* testverticalcells.c
2  *
3  * Copyright (C) 2010 Openismus GmbH
4  *
5  * Authors:
6  *      Tristan Van Berkom <tristanvb@openismus.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "config.h"
23 #include <gtk/gtk.h>
24
25 typedef struct _TreeEntry TreeEntry;
26
27 struct _TreeEntry {
28   const gchar *icon;
29   const gchar *info;
30   const gchar *description;
31   const gchar *fine_print;
32   const gchar *fine_print_color;
33   gint progress;
34   TreeEntry *entries;
35 };
36
37 enum {
38   ICON_COLUMN,
39   INFO_COLUMN,
40   DESCRIPTION_COLUMN,
41   FINE_PRINT_COLUMN,
42   FINE_PRINT_COLOR_COLUMN,
43   PROGRESS_COLUMN,
44   NUM_COLUMNS
45 };
46
47
48 static TreeEntry info_entries[] =
49   {
50     { 
51       "gtk-execute", 
52       "Will you\n"
53       "run this ?", 
54       "Currently executing that thing... you might want to abort",
55       "and every day he went fishing for pigs in the sky",
56       "green",
57       83,
58       NULL
59     },
60     { 
61       "gtk-dialog-authentication", 
62       "This is the\n"
63       "realest of the real", 
64       "We are about to authenticate the actual realness, this could take some time",
65       "one day he caught a giant ogre who barked and barked and barked",
66       "purple",
67       4,
68       NULL
69     },
70     { 0, },
71   };
72
73 static TreeEntry directory_entries[] =
74   {
75     { 
76       "gtk-edit", 
77       "We can edit\n"
78       "things in here", 
79       "Time to edit your directory, almost finished now",
80       "she thought the best remedy for daydreams was going to be sleep",
81       "dark sea green",
82       99,
83       NULL
84     },
85     { 
86       "gtk-file", 
87       "You have a\n"
88       "file here", 
89       "Who would of thought there would be a file in the directory ?",
90       "after taking loads of sleeping pills he could still hear the pigs barking",
91       "green yellow",
92       33,
93       NULL
94     },
95     { 
96       "gtk-dialog-question", 
97       "Any questions ?",
98       "This file would like to ask you a question",
99       "so he decided that the fine print underneath the progress bar probably made no sense at all",
100       "lavender",
101       73,
102       NULL
103     },
104     { 0, },
105   };
106
107 static TreeEntry other_entries[] =
108   {
109     { 
110       "gtk-zoom-fit", 
111       "Thats the\n"
112       "perfect fit", 
113       "Now fitting foo into bar using frobnicator",
114       "using his nifty wide angle lense, he was able to catch a 'dark salmon', it was no flying pig "
115       "however it was definitely a keeper",
116       "dark salmon",
117       59,
118       NULL
119     },
120     { 
121       "gtk-underline", 
122       "Under the\n"
123       "line", 
124       "Now underlining that this demo would look alot niftier with some real content",
125       "it was indeed strange to catch a red salmon while fishing for pigs in the deep sky blue.",
126       "deep sky blue",
127       99,
128       NULL
129     },
130     { 0, },
131   };
132
133 static TreeEntry add_entries[] =
134   {
135     { 
136       "gtk-about", 
137       "its about\n"
138       "to start", 
139       "This is what it's all about",
140       "so he went ahead and added the 'gtk-about' icon to his story, thinking 'mint cream' would be the "
141       "right color for something like that",
142       "dark violet",
143       1,
144       NULL
145     },
146     { 
147       "gtk-zoom-in", 
148       "Watch it\n"
149       "Zoom !", 
150       "Now zooming into something",
151       "while fishing for pigs in the sky, maybe he would have caught something faster if only he had zoomed in",
152       "orchid",
153       6,
154       NULL
155     },
156     { 
157       "gtk-zoom-out", 
158       "Zoom Zoom\n"
159       "Zoom !", 
160       "Now zooming out of something else",
161       "the daydream had a much prettier picture over all once he had zoomed out for the wide angle, "
162       "jill agreed",
163       "dark magenta",
164       46,
165       other_entries
166     },
167     { 0, },
168   };
169
170
171 static TreeEntry main_entries[] =
172   {
173     { 
174       "gtk-info", 
175       "This is all\n"
176       "the info", 
177       "We are currently informing you",
178       "once upon a time in a land far far away there was a guy named buba",
179       "red",
180       64,
181       info_entries
182     },
183     { 
184       "gtk-dialog-warning", 
185       "This is a\n"
186       "warning", 
187       "We would like to warn you that your laptop might explode after we're done",
188       "so he decided that he must be stark raving mad",
189       "orange",
190       43,
191       NULL
192     },
193     { 
194       "gtk-dialog-error", 
195       "An error will\n"
196       "occur", 
197       "Once we're done here, dont worry... an error will surely occur.",
198       "and went to a see a yellow physiotherapist who's name was jill",
199       "yellow",
200       98,
201       NULL
202     },
203     { 
204       "gtk-directory", 
205       "The directory", 
206       "Currently scanning your directories.",
207       "jill didnt know what to make of the barking pigs either so she fed him sleeping pills",
208       "brown",
209       20,
210       directory_entries
211     },
212     { 
213       "gtk-delete", 
214       "Now deleting\n"
215       "the whole thing",
216       "Time to delete the sucker",
217       "and he decided to just delete the whole conversation since it didnt make sense to him",
218       "dark orange",
219       26,
220       NULL
221     },
222     { 
223       "gtk-add", 
224       "Anything\n"
225       "to add ?",
226       "Now adding stuff... please be patient",
227       "but on second thought, maybe he had something to add so that things could make a little less sense.",
228       "maroon",
229       72,
230       add_entries
231     },
232     { 
233       "gtk-redo", 
234       "Time to\n"
235       "do it again",
236       "For the hell of it, lets add the content to the treeview over and over again !",
237       "buba likes telling his story, so maybe he's going to tell it 20 times more.",
238       "deep pink",
239       100,
240       NULL
241     },
242     { 0, },
243   };
244
245
246 static void
247 populate_model (GtkTreeStore *model,
248                 GtkTreeIter  *parent,
249                 TreeEntry    *entries)
250 {
251   GtkTreeIter iter;
252   gint        i;
253
254   for (i = 0; entries[i].info != NULL; i++)
255     {
256       gtk_tree_store_append (model, &iter, parent);
257       gtk_tree_store_set (model, &iter,
258                           ICON_COLUMN, entries[i].icon,
259                           INFO_COLUMN, entries[i].info,
260                           DESCRIPTION_COLUMN, entries[i].description,
261                           FINE_PRINT_COLUMN, entries[i].fine_print,
262                           FINE_PRINT_COLOR_COLUMN, entries[i].fine_print_color,
263                           PROGRESS_COLUMN, entries[i].progress,
264                           -1);
265
266       if (entries[i].entries)
267         populate_model (model, &iter, entries[i].entries);
268     }
269 }
270
271 static GtkTreeModel *
272 create_model (void)
273 {
274   GtkTreeStore *model;
275   gint          i;
276
277   model = gtk_tree_store_new (NUM_COLUMNS,
278                               G_TYPE_STRING,
279                               G_TYPE_STRING,
280                               G_TYPE_STRING,
281                               G_TYPE_STRING,
282                               G_TYPE_STRING,
283                               G_TYPE_INT);
284
285   for (i = 0; i < 20; i++)
286     populate_model (model, NULL, main_entries);
287
288   return GTK_TREE_MODEL (model);
289 }
290
291 gint
292 main (gint argc, gchar **argv)
293 {
294   GtkWidget *window;
295   GtkWidget *scrolled_window;
296   GtkWidget *tree_view;
297   GtkTreeModel *tree_model;
298   GtkCellRenderer *renderer;
299   GtkTreeViewColumn *column;
300   GtkCellArea *area;
301   
302   gtk_init (&argc, &argv);
303
304   if (g_getenv ("RTL"))
305     gtk_widget_set_default_direction (GTK_TEXT_DIR_RTL);
306
307   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
308   gtk_window_set_title (GTK_WINDOW (window), "Vertical cells in GtkTreeViewColumn example");
309   g_signal_connect (window, "destroy", gtk_main_quit, NULL);
310
311   scrolled_window = gtk_scrolled_window_new (NULL, NULL);
312   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_ETCHED_IN);
313   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), 
314                                   GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
315   gtk_container_add (GTK_CONTAINER (window), scrolled_window);
316
317   tree_model = create_model ();
318   tree_view = gtk_tree_view_new_with_model (tree_model);
319   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), FALSE);
320
321   /* First column */
322   column = gtk_tree_view_column_new ();
323
324   renderer = gtk_cell_renderer_pixbuf_new ();
325   g_object_set (renderer, "stock-size", GTK_ICON_SIZE_DIALOG, NULL);
326   gtk_tree_view_column_pack_start (column, renderer, TRUE);
327   gtk_tree_view_column_set_attributes (column, renderer,
328                                        "stock-id", ICON_COLUMN, NULL);
329
330   renderer = gtk_cell_renderer_text_new ();
331   g_object_set (renderer, "scale", 1.2F, "weight", PANGO_WEIGHT_BOLD, NULL);
332   gtk_tree_view_column_pack_start (column, renderer, TRUE);
333   gtk_tree_view_column_set_attributes (column, renderer,
334                                        "text", INFO_COLUMN,
335                                        NULL);
336   gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
337
338   /* Second (vertical) column */
339   column = gtk_tree_view_column_new ();
340   area = gtk_cell_layout_get_area (GTK_CELL_LAYOUT (column));
341   gtk_orientable_set_orientation (GTK_ORIENTABLE (area), GTK_ORIENTATION_VERTICAL);
342
343   renderer = gtk_cell_renderer_text_new ();
344   g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, "editable", TRUE, NULL);
345   gtk_tree_view_column_pack_start (column, renderer, TRUE);
346   gtk_tree_view_column_set_attributes (column, renderer,
347                                        "text", DESCRIPTION_COLUMN,
348                                        NULL);
349
350   renderer = gtk_cell_renderer_progress_new ();
351   gtk_tree_view_column_pack_start (column, renderer, TRUE);
352   gtk_tree_view_column_set_attributes (column, renderer,
353                                        "value", PROGRESS_COLUMN,
354                                        NULL);
355
356   renderer = gtk_cell_renderer_text_new ();
357   g_object_set (renderer, "scale", 0.6F, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
358   gtk_tree_view_column_pack_start (column, renderer, TRUE);
359   gtk_tree_view_column_set_attributes (column, renderer,
360                                        "text", FINE_PRINT_COLUMN,
361                                        "foreground", FINE_PRINT_COLOR_COLUMN,
362                                        NULL);
363
364   gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
365
366   gtk_tree_view_expand_all (GTK_TREE_VIEW (tree_view));
367
368   gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);
369   
370   gtk_window_set_default_size (GTK_WINDOW (window),
371                                800, 400);
372
373   gtk_widget_show_all (window);
374   gtk_main ();
375
376   return 0;
377 }