goran Guest
|
Posted: Mon Apr 14, 2003 1:51 pm Post subject: |
|
|
After that you can view my small app recnik (dictionary for translating gnome enviroment on serbian). (http://www.devbase.net/recnik/recnik.tar.gz)
In file src/interface.c you can find code for generating and packing list widget called lista. In file src/callback.c you can find small callback function on_click_nadji that calls another callback function from sqlite that generates store data (store pointer is transfered to another function). After we create our store model, we apply it t our widget that has columns and is packed to container. after that we can see nice two-column list view.
So what we need to do to generate list view:
First, you will need the new GtkWidget so we write:
After that you will need the GtkTreeStore for sotring data for the list. Note that you can update store for the list latter from the code so the list can be dynamic. Store need to have data about our columns.
| Code: |
GtkTreeStore *store = gtk_tree_store_new (2,
G_TYPE_STRING,
G_TYPE_STRING);
|
That means that our list will have two columns, and both will be text type. But API recomends the other way, by generating global numerated list that we can access from all callbacks. That adds some data encapsulation to the code, and cannot be bad. So the enum will be like this:
| Code: |
enum
{
ENG, // column 1 - 0
SRP, // column 2 - 1
COL_NUMBER // number of columns - 2, zar ne?
};
|
You have two columns and the added element COL_NUMBER will have the value of number of columns, what is very practical.
In that aspect, we will use this code for store:
| Code: |
GtkTreeStore *store = gtk_tree_store_new (BR_KOLONA,
G_TYPE_STRING,
G_TYPE_STRING);
|
Now we need to populate store with data. But, we donot need to do that, remember that we can stop here, create widget from the given store structure (pointed by a store pointer), called model, pack a widget and latter, from callback generate a new store (based on our global numerated list). Than all we need to do is to apply the new store to the list that will delete store and put the new one in the widget.
But, for practical reasons I will put data here. So this is the way:
First, we need to have a GtkIter instance, that can be used as a parent parameter to generate a TreeView Widget, but we will not use this here:
| Code: |
GtkTreeIter iter;
gtk_tree_store_append (store, &iter, NULL); // get the new iter...
gtk_tree_store_set (store, &iter,
ENG, "Text data for the first column",
SRP, "Text data for the second column",
-1);
|
That will add data to column with numer 0, and number 1, or ENG and SRP in our numerated list. -1 means that the item will be top item. This can be value of some earlier Iter (we will need to use two neasted Iters for that, example is in API) but we will keep this -1 for now.
Now we can make more data, or skip that...
Now create real widget with our earlier generated Model:
| Code: |
lista = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
|
We can delete store now, as we donot need that. We can get the store from the list with coresponding function if we need the data later.
| Code: |
g_object_unref (G_OBJECT (store));
|
We are creating cell renderers that will specifies how our data from store will be displayed. GTK provides three various renderers, go to API for more info. As we used text data, we will need to use, guess what?, text renderer. Cell renderer is specified for each column and than applyed to our listview widget.
| Code: |
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("This is header for the first column",
renderer,
"text", ENG,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (lista), column);
|
And now repeat the process for the second column:
| Code: |
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes ("Header for the second column",
renderer,
"text", SRP,
NULL);
gtk_tree_view_append_column (GTK_TREE_VIEW (lista), column);
|
All that is now need is to pack our listview to some container, or direct to the window that I assume that you know how to create.
| Code: |
gtk_container_add (GTK_CONTAINER (prostor), lista);
gtk_widget_show (lista);
gtk_widget_set_name (lista, "lista");
|
... where "prostor" is our container
Do you need more info?
PS - You can use lookup_widget(GTK_WIDGET(button), "lista"); for finding widget, and gtk_tree_view_set_model and gtk_tree_view_get_model for accessing store model of the list.
Goran Rakic
C/C++ programmer[/code] |
|