GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Sep 02, 2014 9:11 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: GtkTreeView: Remove Selected Items "C Language"
PostPosted: Sun Jan 13, 2008 6:06 pm 
Offline

Joined: Sun Jan 13, 2008 4:23 pm
Posts: 2
Location: Puerto Rico
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.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 9:16 am 
Offline
Familiar Face

Joined: Tue Feb 19, 2008 10:26 am
Posts: 5
Thank you for this :) It was very useful.


Top
 Profile  
 
 Post subject: Re: GtkTreeView: Remove Selected Items "C Language"
PostPosted: Tue Feb 19, 2013 12:46 am 
Offline

Joined: Wed Sep 19, 2012 3:06 pm
Posts: 3
It's perfect!!!
Thanks a lot elektroglow. It's very helpful!!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group