Re: Change row background on a Treeview



On Fri, 2008-01-25 at 17:02 +0000, Pedro Sousa wrote:
> Jonathon Jongsma wrote:
> > On 1/25/08, Pedro Sousa <pasousa ml gmail com> wrote:
> >   
> >> Hi,
> >>
> >> I want to change the color background in the rows of a treeview,
> >> creating an effect on the type
> >>
> >>     ------------
> >>     |color blue|
> >>     |----------|
> >>     |color red |
> >>     |----------|
> >>     |color blue|
> >>     |----------|
> >>     |color red |
> >>     |----------|
> >>     |   ...    |
> >>     ------------
> >>
> >> Can anyone give me a pointer to documentation or explain how can I do this?
> >>
> >> My best regards
> >> Pedro Sousa
> >>     
> > you might find Gtk::CellRenderer::property_cell_background() useful:
> > http://gtkmm.org/docs/gtkmm-2.4/docs/reference/html/classGtk_1_1CellRenderer.html#e9e9c794b9e66819f1cde4e2604c25ac
> >
> >   
> Anyone has an example? I don't know how to get this property.

There may be other ways to do it, but the attached file shows one way to
accomplish this effect.

-- 
Jonner
// compile with  g++ `pkg-config --cflags --libs gtkmm-2.4` -o cell-background cell-background.cc
#include <gtkmm.h>

class Columns : public Gtk::TreeModel::ColumnRecord
{
    public:
        Columns () { add (name); add (color); add(value); }
        Gtk::TreeModelColumn<Glib::ustring> name;
        Gtk::TreeModelColumn<int> value;
        Gtk::TreeModelColumn<Glib::ustring> color;  // a non-display column to store the color for each row
};

class MyWindow : public Gtk::Window
{
    public:
        MyWindow() :
            liststore (Gtk::ListStore::create (cols))
        {
            // add some random data to the model
            Gtk::TreeModel::iterator treeiter = liststore->append();
            (*treeiter)[cols.name] = "foo";
            (*treeiter)[cols.value] = 10;
            (*treeiter)[cols.color] = "blue";
            treeiter = liststore->append();
            (*treeiter)[cols.name] = "bar";
            (*treeiter)[cols.value] = 20;
            (*treeiter)[cols.color] = "pink";
            treeiter = liststore->append();
            (*treeiter)[cols.name] = "baz";
            (*treeiter)[cols.value] = 30;
            (*treeiter)[cols.color] = "green";

            treeview = Gtk::manage(new Gtk::TreeView (liststore));

            // create a view column for 'name' and associate the 'cell-background'
            // property of this column's cellrenderer with the value from the
            // 'color' column
            Gtk::TreeViewColumn viewcolumn("Name", cols.name);
            Gtk::CellRenderer* cellrenderer = viewcolumn.get_first_cell_renderer ();
            viewcolumn.add_attribute(cellrenderer->property_cell_background (), cols.color);
            treeview->append_column (viewcolumn);

            // do the same for the 'value' column -- cellrenderers are per-
            // column, not per-row, so you have to do this for each column's
            // cellrenderer if you want to change the color of an entire row
            Gtk::TreeViewColumn viewcolumn2("Value", cols.value);
            cellrenderer = viewcolumn2.get_first_cell_renderer ();
            viewcolumn2.add_attribute(cellrenderer->property_cell_background (), cols.color);
            treeview->append_column (viewcolumn2);

            // add it to the window
            add (*treeview);
        }

    private:
        Columns cols;
        Glib::RefPtr<Gtk::ListStore> liststore;
        Gtk::TreeView* treeview;
};

int main (int argc, char** argv)
{
    Gtk::Main app(argc, argv);
    MyWindow win;
    win.show_all ();
    Gtk::Main::run (win);
    return 0;
}


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