Re: How to unselect a row in the TreeView, when it is selected secondtime?



Paul,

The problem is that you when the code calls select(), the behavior of SELECTION_MULTIPLE is still taking effect.

You need a select function (see Gtk::TreeSelection::set_select_function()). A select function gets called before an item gets selected or deselected in a list. The idea is to have this function return true when _you_ call select(), but false any other time (like when an item gets clicked).

bool unmask_selection = false; // put it in your class, (or make it global extern from another file (just kidding))

...

// In your constructor:
list->get_selection()->set_select_function(sigc::mem_fun(this, &MainWindow::select_function));

...

bool
MainWindow::select_function(const Glib::RefPtr<Gtk::TreeModel>&model, const Gtk::TreeModel::Path &path, bool b)
{
    return unmask_selection;
}


// Then your function, but modified a little (modification in bold):

void
on_button_press( GdkEventButton* ev )
{
	Gtk::TreeModel::Path p ;
	Gtk::TreeViewColumn* tvc ;
	int cx ;
	int cy ;

	if(!tree_view->get_path_at_pos((int) ev->x, (int) ev->y,p, tvc, cx, cy)
		return ;
	
	unmask_selection = true;
	if(tree_selection->is_selected(p))
		tree_selection->unselect(p) ;
	else
		tree_selection->select(p) ;
	unmask_selection = false;
}
The downside of this method is that any time you want to change the selection, you have to set unmask_selection=true (and then unmask_selection=false) around whatever code modifies the selection. This method worked for me.

Alan.



Paul Davis wrote:
Update:

This sure isn't working.  I'll get back when I get it figured out a bit better.

Paul

On 4/24/07, Paul Davis <pjdavis engineering uiowa edu> wrote:
  
SaiKamesh,

You're gonna have to track the selection manually.  This will require
you to connect to the signal_button_press.

The basic outline will basically be something like:

//Some necessary values.
Gtk::TreeView* tree_view = ... ;
Glib::RefPtr< Gtk::TreeSelection > selection = .... ;

//Notice the connect_notify() as opposed to connect()
tree_view->signal_button_press_event().connect_notify( ... )

void
on_button_press( GdkEventButton* ev )
{
      Gtk::TreeModel::Path p ;
      Gtk::TreeViewColumn* tvc ;
      int cx ;
      int cy ;

      if( ! tree_view->get_path_at_pos( ( int ) ev->x, ( int ) ev->y,
p, tvc, cx, cy )
     {
         return ;
     }

      if( tree_selection->is_selected( p ) )
     {
         tree_selection->unselect( p ) ;
      }
      else
      {
          tree_selection->select( p ) ;
      }
}

Should be pretty much it unless I'm forgetting something.

Paul

On 4/24/07, SaiKamesh Rathinasabapathy <rsaikamesh gmail com> wrote:
    
Hi,

I have added a tree_view( Gtk::TreeView ) in a window.

What I have to do is, if a row is selected at the first time, then it will
be highlighted. -> Actually we don't need to do anything for this. It will
automatically be highlighted.

If the same row is clicked (or selected) at the second time, the row should
be unselected.

if the same row is clicked (or selected) at the third time, then it sholud
again be highlighted.

I tried to do the above several times. But I could not do it.  Please help
me out to do this!

Thanks in advance!

_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
http://mail.gnome.org/mailman/listinfo/gtkmm-list


      
_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
http://mail.gnome.org/mailman/listinfo/gtkmm-list
  



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]