Re: [gtkmm] TreeView/TreeModel navigation



Am Sam, 2002-11-30 um 01.23 schrieb Chris Vine:
> On Friday 29 November 2002 4:17 am, 3rdShift wrote:
> > Hi,
> >
> > I am re-reading TreeView/TreeModel reference, docmentation and demo
> > examples again and again and can't seem to find a way to navigate
> > through ListStore. With CList I used to get a row number when user
> > selects the row.
> >
> > 1) How do I get the selected row number?
> >
> > In the selection callback I can get to the row, but how far is
> > that row from the begin()?
> >
> > Glib::RefPtr<Gtk::TreeSelection> m_tree_sel_ref;
> > Glib::RefPtr<Gtk::ListStore> m_list_store_ref;
> >
> > Gtk::TreeModel::iterator iter = m_tree_sel_ref->get_selected ();
> > Gtk::TreeModel::Row row = *iter;
> >
> >
> > I tried to subtract iterators just like you can do in STL, but
> > all I get is 0!

You can't do that in the STL unless you have random access iterators,
which is not the rule.

    Gtk::TreePath path (iter);
    int idx = *path.get_indices().begin();

should give you the row index.  A path is just a vector of indices, for
instance: "1:4:2".  For a ListStore, the path always contains just one
index.

> > int row_num = iter - m_list_store_ref->children ().begin ()
> >
> > 2) How can I select the previous row?
> >
> > Gtk::TreeIter is a one-directional forward iterator.
> > Any hints?

Convert to a Gtk::TreePath and use path.prev().

> > 3) How can I know if the TreeIter points to the last row
> > in ListStore?

> Finding out if the iterator returned by 
> Gtk::TreeView::get_selected()->selection() points to the last row seemed 
> rather easier.  Copy it to another Gtk::TreeModel::iterator (or to a 
> Gtk::TreeModel::Children::iterator), increment the copy, and see if it equals 
> Gtk::ListStore::children()->end().  It worked for me, but it wouldn't 
> surprise me if there is a better way.  (I do not understand the error you 
> report: that doesn't seem right, so there may be some other problem with your 
> code).

There are other ways, but the way you described seems to be the simplest
and most efficient since it only involves one iterator increment and no
index access.

BTW you don't need to compare with end() -- just do:

    Gtk::TreeModel::iterator selected = get_selection()->get_selected();

    if(!++selected)
      // was last row, do something about it

--Daniel





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