Error showing widgets from a thread



Hello,

I have a problem where some widgets do not show up after a
gtk_widget_show() in a second thread. Below is a small test case.

In the main program a notebook with two pages is created. On the second
page is a scrolled window with a viewport, a vbox, and a button. In a
second thread a second button is added to the vbox inside the viewport.

If the second page of the notebook is visible during the creation of this
second button everything works as expected. But if the first page is
active during button creation the second button does not show up if I then
switch manually to the second page. If I resize the window, the button
comes up with the correct width, but with a height of 1. If I press the
first button, the second button is immediately shown correctly (without
a need of releasing the first button). The image at

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

illustrates the problem. It shows from left to right the three states.
I tried it with GTK 2.10.0 / GLib 2.12.0 and various older versions down
till version 2.4.

Any idea why the second button is not shown correctly? Or is this
perhaps a GTK bug which I should report at bugzilla?

Thanks in advance,

Frank

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

GtkWidget *vbox;

void* thread_start (void *arg)
{
    GtkWidget *button;

    sleep (5);
    gdk_threads_enter();

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

    gdk_threads_leave();

    printf ("ready\n");
}

int main (int argc, char **argv)
{
    GtkWidget *window, *widget, *notebook;
    pthread_t thread = (pthread_t) NULL;

    g_thread_init (NULL);
    gdk_threads_init();
    gdk_threads_enter();

    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);

    pthread_create (&thread, NULL, thread_start, NULL);

    gtk_main();
    gdk_threads_leave();
    return 0;
}



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