If I understand Right, then this is what happens:
#include <gtk/gtk.h>
static void btn_clbk ( GtkButton *button, GtkBox *box )
{
/*
Now the removable_button has ref_count 1
This is becouse the Box container owns a reference
If we remove the button from the container (box)
then the container drops the reference and ref_count is 0
If ref_cont is 0 the button is destroyed
If the button is destroyed we can not put it back in container
To do that, ne need to take the ownership of the button before we remove the button
To do that we call g_object_ref() and the ref_count increase to 2
After That we can remove the button and the ref_count drops to 1
Next, we put the button back, and the ref_count increase again from 1 to 2
Last, We need to cal g_object_unref do drop it back to 1 the way it was before
*/
g_return_if_fail ( GTK_IS_BUTTON ( button ) );
g_return_if_fail ( GTK_IS_BOX ( box ) );
GtkWidget *removable_button = g_object_get_data ( G_OBJECT ( box ), "btn_key" );
static gint tmp_ref = FALSE;
if ( GTK_IS_BUTTON ( removable_button ) )
{
if ( !tmp_ref )
{
/* increase ref_count from 1 to 2 */
g_object_ref ( removable_button );
g_print ( "\tThe ref_count is %u\n", G_OBJECT ( removable_button )->ref_count );
gtk_container_remove ( GTK_CONTAINER ( box ), GTK_WIDGET ( removable_button ) );
g_print ( "The removable_button was removed.\n" );
g_print ( "\tThe ref_count is %u\n", G_OBJECT ( removable_button )->ref_count );
tmp_ref = TRUE;
}
else
{
gtk_box_pack_end ( GTK_BOX ( box ), removable_button, FALSE, FALSE, 0 );
g_print ( "The removable_button was Added and ref_count is: %u.\n", G_OBJECT ( removable_button )->ref_count );
g_object_unref ( removable_button );
g_print ( "\t\tAnd Now The ref_count is %u\n", G_OBJECT ( removable_button )->ref_count );
tmp_ref = FALSE;
}
}
}
int main ( void )
{
GtkWidget *window;
GtkWidget *box;
GtkWidget *button;
GtkWidget *removable_button;
gtk_init ( NULL, NULL );
/* create the Window */
window = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
gtk_window_set_default_size ( GTK_WINDOW ( window ), 200, 200 );
g_signal_connect ( window, "delete-event", gtk_main_quit, NULL );
g_signal_connect ( window, "destroy", gtk_main_quit, NULL );
/* create the Box */
box = gtk_box_new ( GTK_ORIENTATION_HORIZONTAL, 0 );
gtk_widget_set_valign ( box, GTK_ALIGN_START );
gtk_container_add ( GTK_CONTAINER ( window ), box );
/* this button remove/brings the remuvable_button*/
button = gtk_button_new_with_label ( "Click" );
/* this button should be removed */
removable_button = gtk_button_new_with_label ( "Remove" );
/* The buttons have/are have Floating Point */
/* here we check the clicked signal, and use the box as user data */
g_signal_connect ( button, "clicked", G_CALLBACK ( btn_clbk ), box );
/* paching the buttons */
/* Now the buttons does not have Floating Point anymore */
gtk_box_pack_start ( GTK_BOX ( box ), button, FALSE, FALSE, 0 );
gtk_box_pack_end ( GTK_BOX ( box ), removable_button, FALSE, FALSE, 0 );
/* connect the removable_button with the Box */
g_object_set_data ( G_OBJECT ( box ), "btn_key", removable_button );
gtk_widget_show_all ( window );
gtk_main ();
}
Output:
The ref_count is 2
The removable_button was removed.
The ref_count is 1
The removable_button was Added and ref_count is: 2.
And Now The ref_count is 1
Anyway, I still do not understand why I got that Output in my previews replay 