Contents: Creating tree view
For this tutorial, you'll need glade >= 3.6, since creating non-widget objects is not possible in previous versions.
I would also recommend you to read GtkTreeView tutorial and Micah Carrick's glade tutorial before trying to follow this one, since I won't be discussing underlying principles much.
Do we start glade now? No, not yet. We need to create an outline of our application. We'll create simple application with GtkTreeView. Tree view will have 3 columns:
- "Image" column that will display image and it's name
- "Penetration" column that will display progress bar
- "Description" column with some text
- pixbuf cell renderer for displaying image (in first column)
- text cell renderer for displaying image name (in first column)
- progress cell renderer for progress bar and (in second column)
- another text cell renderer for description (in third column)
Now we can (finally;) start glade.
After startup, a small dialog ask us about some project properties. Most of the options are fine default, but just make sure you select proper gtk version or GtkBuilder will moan badly when trying to create GUI if your version is less that the one specified in builder file. You've been warned;)
![](http://img504.imageshack.us/img504/1888/20090419212310.png)
![](http://img517.imageshack.us/img517/7431/20090419212530.png)
Now we'll scroll down a bit add some data into our model. Stock item ids are taken from here. And since glade hides some of the descriptions, I wrote down my original table here too.
.-----------+----------+---------------------------------.
| Image | Progress | Desc |
+-----------+----------+---------------------------------+
| gtk-add | 12 | Add icon is bad at penetrating. |
| gtk-cdrom | 78 | CD-ROM image fares pretty well. |
| gtk-cut | 98 | Scissors rock!! |
`-----------+----------+---------------------------------'
|
![](http://img194.imageshack.us/img194/8784/editm.png)
Now we'll create a window and connect it's "destroy" signal to gtk_main_quit function, add vbox with two rows to it and pack GtkTreeView into upper section of vbox (lower section is only a placeholder for the next part of the tutorial, where we'll be adding some controls to our application). When we place our tree view into vbox, glade ask us about model. We'll click on "..." and select "datastore" model, click OK and OK again. And controller part of MVC is done too. Now for the last part - creating display components.
![](http://img407.imageshack.us/img407/1024/20090419212847.png)
![](http://img208.imageshack.us/img208/7192/20090419212909.png)
![](http://img172.imageshack.us/img172/1/20090419212955.png)
![](http://img258.imageshack.us/img258/4673/20090419213009.png)
Now we'll open "Hierarchy" tab and start creating columns and renderers. First we need to add column for images and their names. We click on "Add" button and edit newly created GtkTreeViewColumn by renaming it to "imgcol" and setting title to "Image".
![](http://img300.imageshack.us/img300/5345/20090419213047.png)
![](http://img2.imageshack.us/img2/764/20090419213158.png)
![](http://img150.imageshack.us/img150/3270/20090419213622.png)
![](http://img407.imageshack.us/img407/1015/20090419213857.png)
![](http://img300.imageshack.us/img300/9169/20090419213936.png)
/*
* treeview.c - Application for testing builder file.
*
* Compile with:
* gcc -o treeview treeview.c $(pkg-config --cflags --libs gtk+-2.0 gmodule-export-2.0)
*/
#include <gtk/gtk.h>
int
main(
int
argc,
char
**argv )
{
/* Vars */
GtkWidget *window;
GtkBuilder *builder;
GError *error;
/* Initialization */
gtk_init( &argc, &argv );
/* Create builder and load UI file */
builder = gtk_builder_new();
if
( ! gtk_builder_add_from_file( builder,
"treeview.builder"
, &error ) )
{
g_print(
"Error occured while loading UI file!/n"
);
g_print(
"Message: %s/n"
, error->message );
g_free( error );
return
( 1 );
}
/* Get main window and connect signals */
window = GTK_WIDGET( gtk_builder_get_object( builder,
"window1"
) );
gtk_builder_connect_signals( builder, NULL );
/* Destroy builder */
g_object_unref( G_OBJECT( builder ) );
/* Show main window and start main loop */
gtk_widget_show( window );
gtk_main();
return
( 0 );
}
|