Bug? in OptionMenu, when changing text of selected label



Hi All,

I'm not sure if this is a bug, or I'm not doing this correctly.

What I'm trying to do is use an option menu to select an item from a
list. For each entry of the list, the text displayed for that entry can
change (this is done somewhere else in the GUI). So, I need to be able
to dynamically change the text in the labels of the menuitems in the
option menu. All of this works well, except when I try to change the
currently selected item. Then what happens is the option menu blanks
out. However, if I pop up the option menu, the text of the popup
correctly reflects the changed text, and when I pop the entry down,
all is well.

I took the hello-world example and added an option menu, and a single
button (change) that when pressed, will change the text of the first
entry of the option menu. 

This is compiled with gtk-1.2.7, and appears on linux as well as IRIX.

Any thoughts would be greatly appreciated.

Bill

/* example-start helloworld helloworld.c */

#include <gtk/gtk.h>

/* This is a callback function. The data arguments are ignored
 * in this example. More on callbacks below. */

GtkWidget *button1, *button2;
GtkWidget *label;
int count = 0;

void hello( GtkWidget *widget,
            gpointer   data )
{
    g_print ("Hello World\n");
}

void change( GtkWidget *widget,
            gpointer   data )
{

/* 	GtkWidget *label; */
	
/* 	label = GTK_BIN(button1)->child; */

	char str[100];
	sprintf(str,"Count%d",count++);
	
	gtk_label_set_text(GTK_LABEL(label),str);
	
    g_print ("In Changed\n");
}

gint delete_event( GtkWidget *widget,
                   GdkEvent  *event,
		   gpointer   data )
{
    /* If you return FALSE in the "delete_event" signal handler,
     * GTK will emit the "destroy" signal. Returning TRUE means
     * you don't want the window to be destroyed.
     * This is useful for popping up 'are you sure you want to quit?'
     * type dialogs. */

    g_print ("delete event occurred\n");

    /* Change TRUE to FALSE and the main window will be destroyed with
     * a "delete_event". */

    return(TRUE);
}

/* Another callback */
void destroy( GtkWidget *widget,
              gpointer   data )
{
    gtk_main_quit();
}


int main( int   argc,
          char *argv[] )
{
    /* GtkWidget is the storage type for widgets */
    GtkWidget *window;
    GtkWidget *button, *button3;
    GtkWidget *hbox;
    GtkWidget *option;
    GtkWidget *menu;
    
    gtk_init(&argc, &argv);
    
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    
    hbox = gtk_hbox_new (FALSE,5);

    gtk_signal_connect (GTK_OBJECT (window), "delete_event",
						GTK_SIGNAL_FUNC (delete_event), NULL);
    
    gtk_signal_connect (GTK_OBJECT (window), "destroy",
						GTK_SIGNAL_FUNC (destroy), NULL);
    
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);
    
    button = gtk_button_new_with_label ("Hello World");
    
    gtk_signal_connect (GTK_OBJECT (button), "clicked",
						GTK_SIGNAL_FUNC (hello), NULL);
    
    gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
							   GTK_SIGNAL_FUNC (gtk_widget_destroy),
							   GTK_OBJECT (window));
    
    gtk_box_pack_start (GTK_BOX (hbox), button,FALSE,FALSE,0);

    button3 = gtk_button_new_with_label ("Change");
    
    gtk_signal_connect (GTK_OBJECT (button3), "clicked",
						GTK_SIGNAL_FUNC (change), NULL);

    gtk_box_pack_start (GTK_BOX (hbox), button3,FALSE,FALSE,0);
    
    option = gtk_option_menu_new ();
	gtk_widget_set_usize(option,150,25);
	
    menu = gtk_menu_new ();
	gtk_option_menu_set_menu(GTK_OPTION_MENU(option), GTK_MENU(menu));
	
	button1 = gtk_menu_item_new_with_label("Button 1" );
	button2 = gtk_menu_item_new_with_label("Button 2" );

	label = GTK_BIN(button1)->child;

	gtk_menu_append (GTK_MENU (menu), button1);
	gtk_menu_append (GTK_MENU (menu), button2);

	gtk_box_pack_start (GTK_BOX (hbox), option, FALSE, FALSE,0);

    gtk_container_add (GTK_CONTAINER (window), hbox);

    gtk_widget_show_all (window);
    
    gtk_main ();
    
    return(0);
}
/* example-end */

-- 
Dr. William D. McGrory    AeroSoft, Inc.
mcgrory@aerosft.com       Suite 1275
(540) 557-1904            1872 Pratt Drive
(540) 557-1919 (FAX)      Blacksburg, VA 24060


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