Re: Error showing widgets from a thread



Hello,

On Mon, Jul 10, 2006 at 12:45:10PM -0400, Tristan Van Berkom wrote:
> > But if I switch to the second page after the button was created and then
> > wait some seconds (to be sure that any idle handler finished), the button
> > requisition and allocation are:
> >     requisition = {width = 0, height = 0}
> >     allocation = {x = -1, y = -1, width = 1, height = 1}
> > After pressing the first button these values get somehow fixed and the
> > second button shows up.
> > 
> > But I do not have any idea why the requisition/allocation does not work
> > correctly in this case.
> 
> Is the button visible (i.e. did you show it with gtk_widget_show()) ?

Yes, I did. First I created the window with the notebook and a first button
and used "gtk_widget_show_all (window)" on the window. After that in the
thread I created the second button and used "gtk_widget_show_all (button)"
which in turn calls gtk_widget_show(). GTK_WIDGET_VISIBLE() is true after
that.

>      Note that the thread of execution in which the object was
> created/added to its parent is completely arbitrary, the
> created widget/object belongs to "the program" and is part of
> the GTK hierarchy once created/added.

Yes, you are right, thanks. My initial thought was that there might be a
missing lock somewhere. But a simplified test case without the thread (see
below) shows the same behavior. It first creates the first part of the
interface (window > notebook -> scrolled window with viewport -> vbox ->
first button) and shows it with "gtk_widget_show_all (window)". After that
a second button is created, added to the vbox, and shown with
"gtk_widget_show_all()".

Only if the second notebook page is active during creation of the second
button the button is shown correctly. Otherwise the button is only shown
if I press the first button as shown in the image at

    http://www.techfak.uni-bielefeld.de/~floemker/gtk_show.png

Thanks,

Frank

----------------------------------------------------------------------
#include <stdio.h>
#include <gtk/gtk.h>

int main (int argc, char **argv)
{
    GtkWidget *window, *vbox, *widget, *notebook;

    gtk_init (&argc, &argv);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size (GTK_WINDOW (window), 200, 130);

    notebook = gtk_notebook_new ();
    gtk_container_add (GTK_CONTAINER(window), notebook);

    vbox = gtk_vbox_new (FALSE, 0);
    gtk_notebook_append_page (GTK_NOTEBOOK(notebook), vbox, NULL);

    vbox = gtk_vbox_new (FALSE, 0);
    widget = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW(widget), vbox);
    gtk_notebook_append_page (GTK_NOTEBOOK(notebook), widget, NULL);

    widget = gtk_button_new_with_label ("Test1");
    gtk_box_pack_start(GTK_BOX(vbox), widget, FALSE, FALSE, 5);

    gtk_widget_show_all (window);

    /* Works correctly if this is done:
       gtk_notebook_set_page (GTK_NOTEBOOK(notebook), 1); */

    widget = gtk_button_new_with_label ("Test");
    gtk_box_pack_start (GTK_BOX(vbox), widget, FALSE, FALSE, 5);
    gtk_widget_show_all (widget);

    printf ("VISIBLE(button) %d\n", GTK_WIDGET_VISIBLE(widget));

    gtk_main();
    return 0;
}



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