Widget not showing up in frame

Please see the following small piece of code.

$ cat frame.c 
#include <gtk/gtk.h>

GtkWidget *frame1, *frame2, *box1, *box2, *label1, *label2;
gboolean flag = TRUE;

gboolean button_clicked_callback( GtkWidget *button, gpointer *param )
{
	g_object_ref( box1 );
	g_object_ref( box2 );
	gtk_widget_unparent( box1 );
	gtk_widget_unparent( box2 );
	if( flag )
	{
		gtk_frame_set_child( (GtkFrame*)frame1, box2 );
		gtk_frame_set_child( (GtkFrame*)frame2, box1 );
	}
	else
	{
		gtk_frame_set_child( (GtkFrame*)frame1, box1 );
		gtk_frame_set_child( (GtkFrame*)frame2, box2 );
	}
	flag = !flag;
	return true;
}

static void activate( GtkApplication *app, gpointer user_data )
{
	GtkWidget *window;
	window = gtk_application_window_new( app );
	GtkWidget *main_box = gtk_box_new( GTK_ORIENTATION_HORIZONTAL, 10 );
	gtk_window_set_child( (GtkWindow*)window, main_box );

	frame1 = gtk_frame_new( "frame one" );
	gtk_box_append( (GtkBox*)main_box, frame1 );
	box1 = gtk_box_new( GTK_ORIENTATION_HORIZONTAL, 10 );
	gtk_frame_set_child( (GtkFrame*)frame1, box1 );
	label1 = gtk_label_new("label one");
	gtk_box_append( (GtkBox*)box1, label1 );

	frame2 = gtk_frame_new( "frame two" );
	gtk_box_append( (GtkBox*)main_box, frame2 );
	box2 = gtk_box_new( GTK_ORIENTATION_HORIZONTAL, 10 );
	gtk_frame_set_child( (GtkFrame*)frame2, box2 );
	label2 = gtk_label_new("label two");
	gtk_box_append( (GtkBox*)box2, label2 );

	GtkWidget *button = gtk_button_new_with_label( "shuffle" );
	gtk_box_append( (GtkBox*)main_box, button );
	if( g_signal_connect( G_OBJECT(button), "clicked", G_CALLBACK( button_clicked_callback ), NULL ) <= 0 )
	{
		fprintf( stderr, "Shouldn't be here\n" );
		exit(1);
	}
	gtk_widget_show( window );
}

int main( int argc, char **argv )
{
	GtkApplication *app = gtk_application_new( "hello.one", G_APPLICATION_FLAGS_NONE );
	g_signal_connect( app, "activate", G_CALLBACK( activate ), NULL );
	int status = g_application_run( G_APPLICATION( app ), argc, argv );
	g_object_unref( app );

	return status;
}

If I click shuffle button, contents of frame one are disappearing. But contents of frame two are getting displayed properly.
Not sure if this is a bug in my code OR is there a gap in my understanding OR is it a bug in GTK itself ?

From the gtk_widget_unparent docs:

This function is only for use in widget implementations.

If you want to remove a widget properly from a GtkFrame, use gtk_frame_set_child(f, NULL).

gboolean button_clicked_callback( GtkWidget *button, gpointer *param )
{
	g_object_ref( box1 );
	g_object_ref( box2 );
	gtk_frame_set_child( (GtkFrame*)frame1, NULL );
	gtk_frame_set_child( (GtkFrame*)frame2, NULL );
	if( flag )
	{
		gtk_frame_set_child( (GtkFrame*)frame1, box2 );
		gtk_frame_set_child( (GtkFrame*)frame2, box1 );
	}
	else
	{
		gtk_frame_set_child( (GtkFrame*)frame1, box1 );
		gtk_frame_set_child( (GtkFrame*)frame2, box2 );
	}
	flag = !flag;
	return true;
}

Thanks. Done

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.