Re: [gtkmm] row in a list/treeview



On Mon, 2004-05-10 at 12:12, Andreas B. Thun wrote:
> Thx for your help. I cannot find an example, though.

Attached to this mail you'll find a small piece of source which (among
other things ) will show you how to apply colors to certain cells. I
don't think setting fontproperties will be much different.

Bart


#include <gtkmm/treeview.h>
#include <gtkmm/treestore.h>
#include <gtkmm/window.h>
#include <gtkmm/box.h>
#include <gtkmm/button.h>
#include <gtkmm/main.h>
#include <gtkmm/scrolledwindow.h>

class DemoTree : public Gtk::TreeView
{
  private:
    struct Columns : public Gtk::TreeModelColumnRecord             
    {
      Gtk::TreeModelColumn<Glib::ustring> column1;
      Gtk::TreeModelColumn<Glib::ustring> color;
      Columns() { add(column1);add(color); }
    };
    Columns columns;

    Gtk::CellRendererText* cell_renderer_text;
    Gtk::TreeViewColumn* color_column;
    Gtk::TreeRow row;


  public:
    Glib::RefPtr<Gtk::TreeStore> treestore;

    DemoTree()
    {
      treestore = Gtk::TreeStore::create( columns );
      set_model( treestore );

      append_column("DemoColumn", columns.column1);

      color_column = get_column(0);
      Gtk::CellRenderer* cell_renderer = color_column->get_first_cell_renderer();
      cell_renderer_text = dynamic_cast<Gtk::CellRendererText*>(cell_renderer);
      color_column->add_attribute(cell_renderer_text->property_background(), columns.color);
    }

    void AddRow( const Glib::ustring &name, const Glib::ustring &colour )
    {		
      row = *(treestore->append());
      row[columns.color] = colour;

      row[columns.column1] = name;
    }


};

class DemoWindow : public Gtk::Window
{
  DemoTree dt;
  Gtk::ScrolledWindow scroll_window;
  Gtk::VBox vbox_main;
  Gtk::Button btn_Refresh;

  public:	
  DemoWindow()
  {
    set_default_size( 400, 600 );

    scroll_window.add( dt );
    scroll_window.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
    vbox_main.pack_start( scroll_window );

    btn_Refresh.set_label("Refresh");
    btn_Refresh.signal_clicked().connect( SigC::slot(*this, &DemoWindow::btn_refresh_click) );
    vbox_main.pack_start( btn_Refresh, Gtk::PACK_SHRINK );


    btn_refresh_click();//fill 'er up :P

    add(vbox_main);
    show_all_children();
  }

  void btn_refresh_click()
  {
    dt.treestore->clear();
    for(int i = 0; i < 200; i++) dt.AddRow("TESTING", "blue");
    for(int i = 0; i < 50; i++) dt.AddRow("TESTING", "red");
  }
};


int main(int argc, char *argv[])
{
  Gtk::Main kit(argc, argv);

  DemoWindow dw;
  Gtk::Main::run(dw);
}


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