Re: How to add a custom cell renderer to a Gtk::IconView



Thanks for the reply. I only recently subscribed to the list and need yet to find a searchable archive online, with somewhat usable interface... Anyway, I really appreciate your time in helping me solve this problem.

The only thing I don't know how to do is how to adapt the C/gtk+ renderer to the gtkmm/sigc signal-slot mechanism. My custom renderer has a vfunc declared in the (gtk+ _class) struct as:

void ( *toggled )( GtkCellRendererThumb* renderer, gchar* path, gpointer data );

I obviously need it in order to tell the model when a thumbnail in my icon view has been permanently selected via its checkbox. I tried the following:
    ...
    Gtk::IconView *browser = 0;
    builder->get_widget( "browser", browser );
    {
        GtkIconView *view = browser->gobj();
        GtkCellRenderer *cell = gtk_cell_renderer_thumb_new();
        gtk_cell_layout_pack_start( GTK_CELL_LAYOUT( view ), cell, FALSE );
g_object_set( cell, "thumb-checkable", TRUE, "xpad", 2, "ypad", 2, NULL );
        gtk_cell_layout_set_attributes( GTK_CELL_LAYOUT( view ), cell,
                                        "thumb-name", 0, "thumb-date", 1,
"thumb-image", 2, "thumb-checked", 3,
                                        "thumb-duplicate", 4, NULL );
g_signal_connect( G_OBJECT( cell ), "toggled", G_CALLBACK( &MainWindow::on_thumbnail_checked ), NULL );
    }
    browser->set_model( thumblist );
    ...

The callback is a member function declared as

void on_thumbnail_checked( GtkCellRendererThumb* renderer, gchar* path, gpointer data )
{
    std::cout << "test, path is " << path << std::endl;
}

Everything works up to the call of the function. It is called once, the first time one of the thumbnails is activated, and it prints out "test, path is". Any subsequent click on a thumbnail does not trigger the callback, although the activate function of the renderer itself is called every time - verified thorugh g_print statements, which print out the tree path as well....

Obviously I'm doing something wrong, but what exactly? I could not find any examples that not only mix gtk+ and gtkmm widgets, but also glib and sigc signaling.

I would appreciate an example and/or a hint very much! Thanks in advance.

Best regards,

Todor

On 11/20/2009 12:32 AM, Jos Alburquerque wrote:
On Tue, 2009-11-17 at 22:58 -0500, Todor Todorov wrote:
I need to clarify. What I tried was:

Glib::RefPtr<Gtk::CellLayout>  cl = Glib::wrap( GTK_CELL_LAYOUT( my_icon_view.gobj() ) );


And the error was: "Glib::wrap_auto_interface(): The C++ instance
(N3Gtk8IconViewE) does not dynamic_cast to the interface."


On 11/17/2009 10:47 PM, Todor Todorov wrote:
Hello list,

I encountered the following problem: Under Gtk+ it is possible to have a
GktIconView and cast it to a GtkCellLayout via the GTK_CELL_LAYOUT(obj)
macro. Then I could add a custom cell renderer and set attributes to it.

Now I have implemented my cell renderer in C++/Gtkmm and would like to
add it to my Gtk::IconView. And since the class does not provide a
pack_start() functionality like TreeView, I would have to go and find my
own way to do it...

The first problem is that the IconView class does not inherit (in the
traditional C++ way) from Gtk::CellLayout. Second, the
reinterpret_cast<>  did not work either. There is no function in the
Gtk::IconView API that lets me convert to a CellLayout, or in the
Gtk::CellLayout API to create it from an IconView... Third, I tried to
do something like

Gtk::CellLayout *cl = Glib::wrap( GTK_CELL_LAYOUT( my_icon_view.gobj() ) );
cl->pack_start( renderer, false );
cl->add_attribute( renderer->property_blah(), columns.blah );
....

While it compiles, the application crashed with a reinterpret_cast
failure reported. The only thing that worked so far was to take my Gtk+
implementation of the renderer, add it to my project and create a
GtkIconView, cast it to GTK_CELL_LAYOUT, add the renderer with
properties, and at the end wrap it into a Gtk::IconView and work from
there as it were pure Gtkmm.

I wonder though, is this the best way? I would like to not handle Gtk+
objects directly in my code. Can anyone make a suggestion as to how to
add a custom cell renderer to a Gtk::IconView in a "best practice"
approach? Your help and time are greatly appreciated.
The problem is that Gtk::IconView does not implement the Gtk::CellLayout
interface because the change was done in Gtk+ at a time when gtkmm could
not break ABI (I think searching on the list will provide more info).
For now you're probably doing it the right way until the change can be
made to gtkmm.



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