Re: [gtkmm] ListStore sorting and Gtk::TreeIter validity problems



Chris,

Thank you for your answer, I really appreciate it.

On Sun, 2004-08-01 at 00:49, Chris Vine wrote:
> There should be no problem in principle with 
> store->erase(get_selection()->get_selected())
> 
> There appears to be another problems with your code, which may or may not be 
> connected with this.  You should not store Gtk::TreeIter objects in a 
> container for later use, as they can be invalidated by other operations on 
> the tree model.

I have been assuming that they remain valid for C++, because the GTK API
docs state that the List- and TreeModel iterators remain valid, but that
may be incorrect for C++:

"Additionally, some models guarantee that an iterator is valid for as
long as the node it refers to is valid (most notably the GtkTreeStore
and GtkListStore)."

http://developer.gnome.org/doc/API/2.0/gtk/GtkTreeModel.html

I will try this with the mentioned Gtk::TreeRowReference, I did not know
that one.
Either way...

> Your GtkBookList::remove_book() method is therefore likely to fail.  Have you 
> mis-diagnosed the method in which the problem shows up?

...the iterators are not accessed in the test case. I have now removed
the unnecessary methods and the map (updated files & test case
appended), but the problem still persists.

Any further ideas?

Thanks,
-Samuel
-- 
 ------------------------------------------------------
|      Samuel Abels       |   http://www.debain.org    |
| spam ad debain dod org  | knipknap ad jabber dod org |
 ------------------------------------------------------
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "GtkBookList.h"


GtkBookList::GtkBookList()
{
  // Attach the liststore to the treeview.
  store     = Gtk::ListStore::create(columns);
  sortstore = Gtk::TreeModelSort::create(store);
  sortstore->set_sort_column_id(columns.author_and_title, Gtk::SORT_ASCENDING);
  set_model(sortstore);
  set_rules_hint();
  set_headers_visible(TRUE);
  set_headers_clickable();
  
  // Load the icon.
  pixbuf_book = Gdk::Pixbuf::create_from_file("book.png");
  
  // Create a cell renderer and pack it into the "Author and Title" column.
  Gtk::TreeView::Column* col = Gtk::manage(
                                  new Gtk::TreeView::Column("Autor und Titel"));
  col->pack_start(columns.icon,             FALSE);
  col->pack_start(columns.author_and_title, TRUE);
  
  // Enable pango markup language for the text renderer and change the layout
  // for the icon renderer.
  std::vector<Gtk::CellRenderer*> rends = col->get_cell_renderers();
  rends[0]->set_fixed_size(60, 48);
  col->clear_attributes(*rends[1]);
  col->add_attribute(*rends[1], "markup", 1);
  rends[1]->property_yalign() = 0.3;
  
  // Append all columns.
  append_column(*col);
  get_column(0)->set_resizable(TRUE);
  get_column(0)->set_sort_column_id(0);
  append_column("Kategorie", columns.category);
  get_column(1)->set_resizable(TRUE);
  get_column(1)->set_sort_column_id(1);
  append_column("ISBN",      columns.isbn);
  get_column(2)->set_resizable(TRUE);
  get_column(2)->set_sort_column_id(2);
}


GtkBookList::~GtkBookList()
{
}


void GtkBookList::insert_book(Book* book)
{
  Gtk::TreeIter iter = store->append();
  Gtk::TreeRow  row  = *iter;
  row_fill(row, book);
}


void GtkBookList::remove_selected(void)
{
  //FIXME: This does not work.
  printf("GtkBookList::remove_selected(): Called.\n");
  if (!get_selection()->get_selected())
    return;
  Book* book = get_selection()->get_selected()->get_value(columns.book);
  store->erase(get_selection()->get_selected());
}


void GtkBookList::row_fill(Gtk::TreeModel::Row &row, Book* book)
{
  std::vector<Gtk::CellRenderer*> rends = get_column(0)->get_cell_renderers();
  row[columns.icon]             = pixbuf_book;
  row[columns.author_and_title] = "<b>" + book->get_author() + "</b>\n"
                                + book->get_title();
  row[columns.category]         = book->get_category();
  row[columns.isbn]             = book->get_isbn();
  row[columns.book]             = book;
}
/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
#ifndef _GTKBOOKLIST_H
#define _GTKBOOKLIST_H

#include <gtkmm.h>
#include <iostream>
#include <map>
#include "Book.h"

class GtkBookList : public Gtk::TreeView {
public:
  GtkBookList();
  ~GtkBookList();
  
  /* Triggered whenever a book has been selected. */
  SigC::Signal1<void, Book*> signal_book_selected;
  /* Triggered whenever a book has been activated. */
  SigC::Signal1<void, Book*> signal_book_activated;
  
  /* Inserts the given book. */
  void insert_book(Book* book);
  
  /* Removes the selected book. */
  void remove_selected(void);
  
private:
  /* Fills the given tree row with the data from the given book. */
  void row_fill(Gtk::TreeModel::Row &row, Book* book);
  
  // List model columns.
  class ModelColumns : public Gtk::TreeModel::ColumnRecord {
  public:
    Gtk::TreeModelColumn<Glib::RefPtr<Gdk::Pixbuf> > icon;
    Gtk::TreeModelColumn<Glib::ustring>              author_and_title;
    Gtk::TreeModelColumn<Glib::ustring>              category;
    Gtk::TreeModelColumn<Glib::ustring>              isbn;
    Gtk::TreeModelColumn<Book*>                      book;
    ModelColumns() {
      add(icon);
      add(author_and_title);
      add(category);
      add(isbn);
      add(book);
    }
  };
  Glib::RefPtr<Gtk::ListStore>     store;
  Glib::RefPtr<Gtk::TreeModelSort> sortstore;
  Gtk::CellRendererText            renderer_text;
  Glib::RefPtr<Gdk::Pixbuf>        pixbuf_book;
  ModelColumns                     columns;
};

#endif /* _GTKBOOKLIST_H */

Attachment: booklist.tar.bz2
Description: application/bzip-compressed-tar



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