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 

GtkTreeView: Remove Selected Items "C Language"

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code
Author Message
elektroglow



Joined: 13 Jan 2008
Posts: 2
Location: Puerto Rico

PostPosted: Sun Jan 13, 2008 6:06 pm    Post subject: GtkTreeView: Remove Selected Items "C Language" Reply with quote

Hi!, im new here :)...


ok..
the code is simple and clear...
hope it will be usefull for u guys...

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

void remove_selected_items ( GtkTreeView *treeview )
{
    GtkTreeSelection *selection = gtk_tree_view_get_selection ( treeview );
    GtkListStore *store;
    GtkTreeModel *model;
    GtkTreeIter iter;
   
    if (gtk_tree_selection_count_selected_rows(selection) == 0)
        return;
   
    GList *list = gtk_tree_selection_get_selected_rows( selection, &model );
    store = GTK_LIST_STORE ( model );
   
    int nRemoved = 0;
    while(list) {
        int ipath = atoi(gtk_tree_path_to_string(list->data));
        ipath-=nRemoved;
        GString *fixed_path = g_string_new("");
        g_string_printf(fixed_path, "%d", ipath);
       
        GtkTreePath *path = gtk_tree_path_new_from_string(fixed_path->str);
        g_string_free(fixed_path, TRUE);
       
        if (path) {
            //g_printf("%d\n", ipath);
           
if ( gtk_tree_model_get_iter ( model, &iter, path) ) { // get iter from specified path
               
gtk_list_store_remove ( store, &iter ); // remove item
               
nRemoved++;   
            }
            else { // invalid path
               
g_error("Error!!!\n");
            }
            gtk_tree_path_free (path);
        }
        else {
            g_error("Error!!!\n");
        }
        list = list->next;
    }
    g_list_foreach (list, (GFunc)gtk_tree_path_free, NULL);
    g_list_free (list);
}

void
on_btnRemoveSelectedItems_clicked (GtkButton *button, gpointer user_data)
{
    /*=====================================
    * This is optional. Depends on how you are using this code. */
   
GtkTreeView *treeview = GTK_TREE_VIEW (lookup_widget ( myWindow, "myTreeView" ));
    /*=====================================*/

    // send a reference of your tree view
   
remove_selected_items ( treeview );
}


Remember to setup your treeview with multiple-selection support,
otherwise you will be able to just remove one row at a time.
Code: (C)
1
2
GtkTreeSelection *selection = gtk_tree_view_get_selection ( GTK_TREE_VIEW(treeview) );
gtk_tree_selection_set_mode ( selection, GTK_SELECTION_MULTIPLE );
I do this after column creation.
Back to top
opique
Familiar Face


Joined: 19 Feb 2008
Posts: 5

PostPosted: Wed Feb 27, 2008 9:16 am    Post subject: Reply with quote

Thank you for this :) It was very useful.
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