Re: Problem with reparent (Gtk+-2.12)



В Срд, 06/02/2008 в 20:28 +0200, Tor Lillqvist пишет:
> Do you think you could come up with a corresponding test program in,
> dare I say it, plain C?
> 
> --tml

My bad, I forgot to attach an example file.
Sorry!

There it is.

Regards,
-andrew

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

GtkWidget* gApp = 0;
GtkWidget* gNotebook = 0;
GtkTooltips* gTooltips = 0;

typedef struct UserData_
{
    GtkWidget* oldParent;
    gboolean isDocked;
}
UserData;

void button_dock_cb( GtkWidget* widget, gpointer data )
{
    GtkWidget* vbox = GTK_WIDGET( data );
    UserData* ud = ( UserData* )g_object_get_data( G_OBJECT( vbox ), "parent_info" );

    if( ud->isDocked == TRUE )
    {
	ud->isDocked = FALSE;
	GtkWidget* parent = vbox->parent;
	gtk_widget_reparent( vbox, ud->oldParent );
	gtk_notebook_remove_page( GTK_NOTEBOOK( gNotebook ),
				  gtk_notebook_page_num( GTK_NOTEBOOK( gNotebook ), parent ) );

	gtk_widget_show( ud->oldParent );
    }
    else
    {
	GtkWidget* frame = gtk_frame_new( NULL );
	gtk_widget_show( frame );

	ud->isDocked = TRUE;
	gtk_notebook_append_page( GTK_NOTEBOOK( gNotebook ), frame, NULL );
	gtk_widget_reparent( vbox, frame );
	gtk_widget_hide( vbox );
	gtk_widget_show( vbox );

	gtk_widget_hide( ud->oldParent );
    }
}

void toolitem_clicked_cb( GtkWidget* widget, gpointer data )
{
    printf( "Toolbar Item %p clicked\n", data );
}

void create_new_window( GtkWidget* widget, gpointer data )
{
    GtkWidget* vbox;
    GtkWidget* button;
    GtkWidget* toolbar;
    GtkToolItem* item;
    GtkWidget* win = gtk_window_new( GTK_WINDOW_TOPLEVEL );
    UserData* ud;

    vbox = gtk_vbox_new( FALSE, 0 );
    gtk_container_add( GTK_CONTAINER( win ), vbox );

    ud = g_malloc( sizeof( UserData ) );
    ud->isDocked = FALSE;
    ud->oldParent = vbox->parent;
    g_object_set_data( G_OBJECT( vbox ), "parent_info", ( gpointer )ud );

    gtk_widget_show( vbox );

    toolbar = gtk_toolbar_new();
    gtk_toolbar_set_tooltips( GTK_TOOLBAR( toolbar ), TRUE );
    gtk_box_pack_start( GTK_BOX( vbox ), toolbar, TRUE, TRUE, 5 );
    gtk_widget_show( toolbar );

    item = gtk_tool_button_new_from_stock( GTK_STOCK_NEW );
    g_signal_connect( G_OBJECT( item ), "clicked", G_CALLBACK( toolitem_clicked_cb ), ( gpointer )item );
    gtk_toolbar_insert( GTK_TOOLBAR( toolbar ), item, -1 );
    gtk_widget_show( GTK_WIDGET( item ) );
    gtk_toolbar_set_style( GTK_TOOLBAR( toolbar ), GTK_TOOLBAR_ICONS );

    button = gtk_button_new_with_label( "dock/undock" );
    g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( button_dock_cb ), ( gpointer )vbox );
    gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, FALSE, 10 );
    gtk_widget_show( button );

    gtk_widget_show( win );
}

GtkWidget* create_app_window()
{
    GtkWidget* vbox;
    GtkWidget* button;
    GtkWidget* win = gtk_window_new( GTK_WINDOW_TOPLEVEL );

    vbox = gtk_vbox_new( FALSE, 5 );
    gtk_container_add( GTK_CONTAINER( win ), vbox );
    gtk_widget_show( vbox );

    gNotebook = gtk_notebook_new();
    gtk_notebook_set_tab_pos( GTK_NOTEBOOK( gNotebook ), GTK_POS_TOP );
    gtk_box_pack_start( GTK_BOX( vbox ), gNotebook, TRUE, TRUE, 0 );
    gtk_widget_show( gNotebook );

    button = gtk_button_new_with_label( "new window" );
    g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( create_new_window ), NULL );
    gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, FALSE, 0 );
    gtk_widget_show( button );

    gtk_widget_show( win );

    return win;
}

int main( int argc, char* argv[] )
{

    gtk_init( &argc, &argv );

    gApp = create_app_window();
    g_signal_connect( G_OBJECT( gApp ), "delete_event", G_CALLBACK( gtk_main_quit ), NULL );

    gtk_main();

    return 0;
}


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