Search code examples
c++gtkgtkmm

gtkmm treemodel adding rows


I'm somewhat confused as to how this ListStore works in this tutorial http://developer.gnome.org/gtkmm-tutorial/3.0/sec-treeview-examples.html.en

I understand pretty much everything except for the row[m_Columns.m_ITEM] = value

My issue is how it knows which column in the row to set the value to based on some other object that is passed to the overloaded []. Does it just check if the types are the same? If so then what happens if you have two columns of the same type? Does it work based on memory offsets to figure out which column it is?

I'm having a tough time figuring out how it goes from giving a member of the m_Columns instance to figuring out which column it should put the value in.

I guess if I could just get someone to explain how the internals work on the treemodel system that would be great, like what happens with creating and then each step of adding new rows and what not.


Solution

  • There's a bit of template trickery going on. The array operator in TreeRow is overloaded for each instantiation of TreeModelColumn<ColumnType>. To allow you to assign to the column as well as read it, it returns a proxy object (TreeValueProxy<ColumnType>) which actually implements those operations. Proxy objects are a standard C++ technique to implement array-like objects. The proxy contains references to the relevant TreeRow and TreeModelColumn<ColumnType> objects which it uses to do the actual work.

    The TreeModelColumn<ColumnType> object also stores a gobject type corresponding to the type of the column, as well as an integer corresponding to the column position. This is eventually used to interface with the gtk+ library starting in TreeRow::set_value<ColumnType>() and TreeRow::get_value<ColumnType>().

    The source files to look at if you want the messy details are here for TreeRow and here for TreeModelColumn.