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 );
}
|