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

Re: gtk_container_remove



> Hi,
>
> I'm trying to create a UI whereby you can choose (from a combo box) an
> option, and each time you choose a different option, a new set of UI
> widgets is shown (replacing what was shown before). (such as in the
> GNOME control center).
>
> I though that to do this all I would have to do is do something like:
>
> gtk_container_remove( my_container, widget);
>
> and remove the old vbox widget I had stored there, and replaced it by:
>
> gtk_container_add( my_container, newwidget);
>
> Ok, so this works, I can change what widget is shown, except when I try
> to change the widget shown back to one I've already had. I get an
> assertion failure because my widget has suddenly become not a widget. I
> think it is because gtk_container_remove is deleting the widget when it
> is removed?

    When you remove a widget from its container, its reference count
    is decremented. When the ref count is 0, the widget is destroyed, so you
    cannot use it anymore.

    The correct workaround for this is to ref the widget with the
appropriate
    function before removing it from its container, then unref it when you
    add it back to a container (the same one or another one, it should not
    matters)

    So you have your remove code:

            gtk_widget_ref(w);
            tag_removed_widget(w);
            gtk_container_remove(c, w);

    and you add count

            gtk_container_add(c, w);
            if (widget_has_remove_tag(w))
                gtk_widget_unref(w);

    For more about refcounting, you should read the
    ${GTKSRC}\docs\refcounting.txt file.

    Hope this helps,

    Emmanuel

> any ideas as to why I can't change back to a widget I have had before?
>
> Also, is this the best way to go about it? I couldn't see anything more
> elegant
>
> cheers
> --
>
> Jamie Love
>   jlove@clear.net.nz
>   jdl28@student.canterbury.ac.nz




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