1
<chapter id="gtk-migrating-GtkIconView">
3
<title>Migrating from GnomeIconList to GtkIconView</title>
6
Since version 2.6, GTK+ provides the #GtkIconView widget. It is similar in
7
functionality to the <structname>GnomeIconList</structname> widget in the
8
libgnomeui library, both widgets provide a way to lay out named icons in
9
a grid. The distinctive feature of the GTK+ widget is that it follows the
10
model-view pattern, allowing it to share the actual data (i.e. the names
11
and images of the icons) with other views.
15
#GtkIconView currently doesn't support some features found in
16
<structname>GnomeIconList</structname>. Icons can not be positioned freely,
17
the spacing is not customizable, and it is not possible to edit the names of
22
To convert an application that uses <structname>GnomeIconList</structname>
23
to #GtkIconView, the first step is to organize your data in a #GtkTreeModel.
24
<structname>GnomeIconList</structname> lets you directly insert data with
25
gnome_icon_list_insert() and gnome_icon_list_insert_pixbuf() and their
26
append variants. So, if you previously had a function to fill your icon
27
list similar to this one:
28
<informalexample><programlisting>
30
fill_icon_list (GnomeIconList *icon_list)
32
gnome_icon_list_append (icon_list, "file1.png", "Icon 1");
33
gnome_icon_list_append (icon_list, "file2.png", "Icon 2");
37
</programlisting></informalexample>
38
you will have to create a tree model, attach your icon view to it, and
40
<informalexample><programlisting>
45
/* you can have more columns here, e.g */
51
fill_model (GtkListStore *store)
56
gtk_list_store_append (store, &iter);
57
pixbuf = gdk_pixbuf_new_from_file ("file1.png", NULL);
58
gtk_list_store_set (store, &iter, PIXBUF_COLUMN, pixbuf, TEXT_COLUMN, "Icon 1", -1);
59
g_object_unref (pixbuf);
61
gtk_list_store_append (store, &iter);
62
pixbuf = gdk_pixbuf_new_from_file ("file2.png", NULL);
63
gtk_list_store_set (store, &iter, PIXBUF_COLUMN, pixbuf, TEXT_COLUMN, "Icon 2", -1);
64
g_object_unref (pixbuf);
70
main (int argc, char *argv[])
75
gtk_init (&argc, &argv);
77
/* do other initialization... */
79
/* construct the GtkIconView */
80
icon_view = gtk_icon_view_new (<!-- -->);
81
store = gtk_list_store_new (3, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_POINTER);
83
gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (icon_view), PIXBUF_COLUMN);
84
gtk_icon_view_set_text_column (GTK_ICON_VIEW (icon_view), TEXT_COLUMN);
85
gtk_icon_view_set_model (GTK_ICON_VIEW (icon_view), GTK_TREE_MODEL (store));
91
</programlisting></informalexample>
92
This example uses a #GtkListStore as model, but part of the elegance of the
93
model-view pattern is that you can easily use another tree model implementation,
94
or even write your own custom tree model.
98
Your application may make use of extra data attached to the icons in the
99
<structname>GnomeIconList</structname> via gnome_icon_list_set_icon_data() and
100
gnome_icon_list_get_icon_data(). With #GtkIconView such data is most
101
conveniently stored in an extra column in the tree model, so you would
103
<informalexample><programlisting>
105
set_icon_data (GtkIconView *icon_view,
112
model = gtk_icon_view_get_model (icon_view);
114
if (gtk_tree_model_iter_nth_child (model, &iter, NULL, idx))
115
gtk_list_store_set (GTK_LIST_STORE (model), &iter,
116
DATA_COLUMN, data, -1);
118
</programlisting></informalexample>
119
assuming that your tree model has a <literal>DATA_COLUMN</literal> of type
124
There is a number of minor API differences between
125
<structname>GnomeIconList</structname> and
126
<structname>GtkIconView</structname>:
129
<typename>GnomeIconListMode</typename> is replaced by the
130
<link linkend="GtkIconView--orientation">orientation</link>
131
property of <structname>GtkIconView</structname>
134
<structname>GtkIconView</structname> can not be frozen in the same
135
way as <structname>GnomeIconList</structname> can with
136
gnome_icon_list_freeze() and gnome_icon_list_thaw(). Instead you can
137
replace the whole model of a <structname>GtkIconView</structname>,
138
instead of doing many small changes to the existing model.
147
sgml-parent-document: ("gtk-docs.sgml" "book" "part" "chapter")