GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Basic List View using GtkTreeView and GtkTreeStore (C)

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code
Author Message
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 427
Location: Portland, OR USA

PostPosted: Wed Nov 28, 2007 5:14 am    Post subject: Basic List View using GtkTreeView and GtkTreeStore (C) Reply with quote

Someone asked how to create a simple list view. Here's a demo. It uses the GtkTreeView with a GtkListStore.

I threw this together in a few minutes after a couple beers... so let me know if I did something stupid and I'll fix it later.

You can find out which item is selected using gtk_tree_view_get_selection() function.


Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

/*
main.c
Eexample of using a GtkTreeView as a simple list view.

Written by Micah Carrick for www.gtkforums.com

Compile:
cc -Wall -g main.c -o example `pkg-config --cflags --libs gtk+-2.0`
*/

#include <gtk/gtk.h>

enum
{
  LIST_ITEM = 0,
  N_COLUMNS
};

static void init_list (GtkWidget *list);
static void add_to_list (GtkWidget *list, const gchar *str);
static void remove_from_list_by_value (GtkWidget *list, const gchar *str);
static void remove_from_list (GtkWidget *list, gint index);

int main (int argc, char *argv[])
{

    GtkWidget           *window, *list, *sw;

    gtk_init (&argc, &argv);

    /* create widgets */
   
   
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    sw = gtk_scrolled_window_new (NULL, NULL);
    list = gtk_tree_view_new ();
   
    /* customize appearance */
   
   
gtk_window_set_title (GTK_WINDOW (window), "List View");
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);
    gtk_widget_set_size_request (window, 250, 150);
   
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),

            GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
            GTK_SHADOW_ETCHED_IN);
   
    gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (list), FALSE);
   
    gtk_container_add (GTK_CONTAINER (sw), list);
    gtk_container_add (GTK_CONTAINER (window), sw);

    /* initialize */

   
init_list (list);
    add_to_list (list, "Testing #1");
    add_to_list (list, "Testing #2");
    add_to_list (list, "Testing #3");
    add_to_list (list, "Testing #4");
    add_to_list (list, "Testing #5");
   
    remove_from_list_by_value (list, "Testing #2");     /* remove first match */
   
remove_from_list (list, 3);                         /* remove the 4th item */
   
    /* #1, #3, and #5 should remain */
   
    /* connect signals */
   
g_signal_connect (G_OBJECT (window), "destroy",
            G_CALLBACK (gtk_main_quit), NULL);

    /* show window */

   
gtk_widget_show_all (window);

    gtk_main ();

    return 0;
}

static void
init_list (GtkWidget *list)
{

    GtkCellRenderer         *renderer;
    GtkTreeViewColumn       *column;
    GtkListStore            *store;

    /* create and append the single column */
   
   
renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes ("List Item",
            renderer, "text", LIST_ITEM, NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (list), column);

    /* create the model and add to tree view */
   
   
store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING);

    gtk_tree_view_set_model (GTK_TREE_VIEW (list), GTK_TREE_MODEL (store));
   
    /* free reference to the store */
   
   
g_object_unref (store);
}

static void
add_to_list (GtkWidget *list, const gchar *str)
{
    GtkListStore            *store;
    GtkTreeIter             iter;
   
    store = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (list)));
   
    gtk_list_store_append (store, &iter);
    gtk_list_store_set (store, &iter, LIST_ITEM, str, -1);
}

static void
remove_from_list_by_value (GtkWidget *list, const gchar *str)
{
    GtkTreeModel            *model;
    GtkTreeIter             iter;
    gboolean                r;
    gchar                   *value;
   
    model = gtk_tree_view_get_model (GTK_TREE_VIEW (list));
   
    /*
    iterate model looking for str and then remove it... only removes the first
    one found. 
    */
   
   
if (gtk_tree_model_get_iter_first (model, &iter) == FALSE) return;
    for (r = gtk_tree_model_get_iter_first (model, &iter);
         r == TRUE;
         r = gtk_tree_model_iter_next (model, &iter))
    {               
        gtk_tree_model_get (model, &iter, LIST_ITEM, &value,  -1);
       
        if (g_ascii_strcasecmp(value, str) == 0)
        {
            gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
            break;
        }
       
        /* free the value once the compare is done */
       
if (value) g_free (value);       
    }
}

static void
remove_from_list (GtkWidget *list, gint index)
{
    GtkTreeModel            *model;
    GtkTreeIter             iter;
    gint                    i;
   
    model = gtk_tree_view_get_model (GTK_TREE_VIEW (list));
   
    /* iterate model removing by and index (0-based) */
   
   
if (gtk_tree_model_get_iter_first (model, &iter) == FALSE) return;
    for (i=0; i < index; i++)
    {
        /* just do nothing if index is out of range */
       
if (gtk_tree_model_iter_next (model, &iter) == FALSE) return;
    }
   
    gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
}


Last edited by Micah Carrick on Fri Nov 30, 2007 10:04 pm; edited 4 times in total
Back to top
HappyCat
Familiar Face


Joined: 13 Aug 2007
Posts: 5

PostPosted: Thu Nov 29, 2007 1:26 am    Post subject: Reply with quote

I think there is a mistake in the code. The comments indicate that the "remove" function should be 0 indexed, however after you call remove_from_list_by_value, then the remove_from_list with 3 as a parameter, it should remove "Testing #5" instead of "Testing #4" (since after the first remove, #5 is the 4th element in the list). I think this is because
Code: (Plaintext)
1
2
3

    for (i=1; i < index; i++)

in the remove function should be changed to
Code: (Plaintext)
1
2
3

    for (i=0; i < index; i++)

Other than that, I have not found errors in this code. Thank you for this, it has helped relieve a lot of frustration.
Back to top
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 427
Location: Portland, OR USA

PostPosted: Thu Nov 29, 2007 1:31 am    Post subject: Reply with quote

Good call. You're right.
Back to top
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 427
Location: Portland, OR USA

PostPosted: Thu Nov 29, 2007 4:01 pm    Post subject: Reply with quote

I changed the for loop index it in the source listing.
Back to top
duckz



Joined: 08 Apr 2008
Posts: 3

PostPosted: Tue Apr 08, 2008 2:11 pm    Post subject: Reply with quote

is it possible to use data from g_dir_open to be listed in the list?
Back to top
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 427
Location: Portland, OR USA

PostPosted: Tue Apr 08, 2008 2:16 pm    Post subject: Reply with quote

Yes, you can use g_dir_open and then loop through the names returned from g_dir_read_name and add each one. An entire file browser is built in Andrew Krause' Foundations of GTK+ Development.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP