Re: Trying hard with a custom cellrenderer.



Partially solved:

#include <gtkmm.h>
#include <string>
#include <iostream>


using namespace Gtk;


class URL {
    std::string url_;
public:
    explicit URL(const std::string & url) : url_(url) {}
    const std::string & getAsString() const { return url_; }
};


class URLCellRenderer : public Gtk::CellRendererPixbuf {
private:
    // the property whose value will be set by Gtk before a cell is rendered
    Glib::Property<Glib::ustring> property_url_;
    OffscreenWindow osw_;
    Gtk::Button button_;
    Gtk::Image img_;

public:
    URLCellRenderer() :  Glib::ObjectBase(typeid(URLCellRenderer)),
            Gtk::CellRendererPixbuf(),  property_url_ (*this, "url"),
            img_(Gtk::Stock::MEDIA_PLAY, Gtk::ICON_SIZE_BUTTON)
    {
        button_.set_image(img_);

        osw_.add(button_);
        osw_.show_all();
        property_pixbuf() = osw_.get_pixbuf();
    }

    void     get_size_vfunc  	(Widget&   	 widget,
		const Gdk::Rectangle*  	cell_area,
		int *  	x_offset,
		int *  	y_offset,
		int *  	width,
		int *  	height	
	) const
    {
        *width = property_pixbuf().get_value()->get_width();
        *height = property_pixbuf().get_value()->get_height();
	}
	

    void render_vfunc(Glib::RefPtr<Gdk::Drawable> const & window,
                      Gtk::Widget& widget,
                      Gdk::Rectangle const & background_area,
                      Gdk::Rectangle const & cell_area,
                      Gdk::Rectangle const & expose_area,
                      Gtk::CellRendererState flags)  {
        property_pixbuf() = osw_.get_pixbuf();
        CellRendererPixbuf::render_vfunc(window, widget,
background_area, cell_area, expose_area, flags);
    }
};


class Cols : public Gtk::TreeModelColumnRecord {
public:
    Cols() { add(name); add(url); }

    TreeModelColumn<Glib::ustring> name;
    TreeModelColumn<Glib::ustring> url;
};


int main (int argc, char* argv[])
{
    Gtk::Main m(&argc, &argv);
    Cols c;
    URL location("http://www.uji.es";);

    auto store = ListStore::create(c);
    TreeView view(store);
    CellRendererText cellr;
    URLCellRenderer cellr2;

    TreeViewColumn tvcol("Name");
    TreeViewColumn tvcol2("Location");

    view.append_column(tvcol);
    view.append_column(tvcol2);

    tvcol.pack_start(cellr);
    tvcol.add_attribute(cellr, "text", c.name);

    tvcol2.pack_start(cellr2, false);
    //tvcol2.add_attribute(cellr2, "url", c.url);

    view.set_headers_visible(true);
    Gtk::Window window;
    window.add(view);


    auto iter = store->append();
    iter->set_value(c.name, Glib::ustring("Germán"));
    iter->set_value(c.url, Glib::ustring(location.getAsString()));

    window.show_all();
    Main::run(window);
    return 0;
}


If someone could tell me how to use a Gtk::TreeModelColumn<URL *>
instead of Glib::ustring I would be happy. This works, but
it's not exactly what I had in my head. Thanks.


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