Author Topic: GTK-- and Glade: TreeViews  (Read 21537 times)

Fenrir

  • Rogueliker
  • ***
  • Posts: 473
  • Karma: +1/-2
  • The Monstrous Wolf
    • View Profile
GTK-- and Glade: TreeViews
« on: November 12, 2009, 02:58:19 AM »
Anyone here ever worked with GTK-- and Glade? I'm having a hell of a time trying to figure out how to add rows to a TreeView widget. There's plenty of documentation, but none of it seems to relate in any way to what I'm trying to do.

EDIT: I didn't think I should put this in development since I'm not working on a roguelike. Apologies if it's in the wrong place.

corremn

  • Rogueliker
  • ***
  • Posts: 700
  • Karma: +0/-0
  • SewerJack Extraordinaire
    • View Profile
    • Demise RogueLike Games
Re: GTK-- and Glade: TreeViews
« Reply #1 on: November 12, 2009, 03:49:55 AM »
Haha, I had a very similar experience with Qt and QtDesigner just resently with their idea of TreeView widgets.   Oh well, just keep at it I guess.  Anyway I have never used GTK, and I guess I wont now Qt is free.
corremn's Roguelikes. To admit defeat is to blaspheme against the Emperor.  Warhammer 40000 the Roguelike

nhdaniel

  • Newcomer
  • Posts: 12
  • Karma: +0/-0
    • View Profile
Re: GTK-- and Glade: TreeViews
« Reply #2 on: November 12, 2009, 08:41:50 AM »
Dunno about the Glade side of things, but you can find an example of a treeview in Interhack project (fully turn based multi-player). (http://sourceforge.net/projects/interhack/). Look in src/interfaces/gtkmm.cc. Summary:

The following are the defn:

Code: [Select]
    /* For the inventory list */
    class ModelColumns : public Gtk::TreeModel::ColumnRecord {
    public:
        ModelColumns(void) { add(name); add(obj); add(order); }
        Gtk::TreeModelColumn<Glib::ustring> name;
        Gtk::TreeModelColumn< Pointer<Object> > obj;
        Gtk::TreeModelColumn<unsigned int> order;
    } modelColumns;
    Gtk::TreeView treeView;
    Glib::RefPtr<Gtk::TreeStore> treeStore;
    Gtk::TreeModel::Row weapons, armor, food, potions, scrolls,
                        spellbooks, rings, wands, tools, amulets, gems;
    Gtk::Window invWindow;
    Gtk::ScrolledWindow invSWindow;

Next we setup the who thing:

Code: [Select]
    /* Inventory Window */
    treeStore = Gtk::TreeStore::create(modelColumns);
    treeView.set_model(treeStore);
    treeView.append_column("Name", modelColumns.name);
    treeView.get_column(0)->set_sort_column(modelColumns.order);
    invSWindow.add(treeView);
    invWindow.add(invSWindow);
    invWindow.show_all();
    invWindow.set_title("Interhack Inventory");

And finally add some fixed rows:

Code: [Select]
    /* Setup basic groups in inventory window */
    weapons = *(treeStore->append());
    weapons[modelColumns.name] = "Weapons (";
    weapons[modelColumns.order] = 0;
    armor = *(treeStore->append());
    armor[modelColumns.name] = "Armor [";
    armor[modelColumns.order] = 1;

Then when you want to add a sub-row:

Code: [Select]
Gtk::TreeModel::Row row = *(treeStore->append(spellbooks.children()));
row[modelColumns.name] = "something";

Fenrir

  • Rogueliker
  • ***
  • Posts: 473
  • Karma: +1/-2
  • The Monstrous Wolf
    • View Profile
Re: GTK-- and Glade: TreeViews
« Reply #3 on: November 12, 2009, 03:03:44 PM »
I tried getting the Gtk::ListStore object out of Glade, but Gtk::Builder::get_object() returns Glib::Object, so Glib::RefPtr<Gtk::ListStore> pWordList = refBuilder->get_object("wordstore") complains that it can't convert Glib::Object to Gtk::ListStore.

EDIT: A Google search for "gtkmm get_object" revealed that I needed to use Glib::RefPtr<Gtk::ListStore> pWordList = Glib::RefPtr<Gtk::ListStore>::cast_static(refBuilder->get_object("wordstore")). If I can just figure out how to get a Gtk::TreeModelColumn out of the bloody thing I'll be in business.

EDIT AGAIN: Haha! I just had to use set_value().
« Last Edit: November 12, 2009, 11:36:14 PM by Fenrir »