Hi!, im new here :)...
ok..
the code is simple and clear...
hope it will be usefull for u guys...
Code:
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:
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.