GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Sep 02, 2014 1:58 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Callback Confusion
PostPosted: Fri Apr 25, 2008 4:37 am 
Offline

Joined: Fri Apr 25, 2008 4:31 am
Posts: 4
I think I have some fundamental misunderstanding with how callbacks work. The following code crashes whenever the callback is called, mostly because I'm not exactly sure how to do what I'm trying to do.

I want to pass the GtkEntry text object to the callback function, but as I said, the application crashes when it gets to this point. If someone could give me an idea of what I'm doing wrong, I'd very much appreciate it.

I understand that this may or may not be how you'd go about this in reality, but I'm just experimenting. What I'm ultimately trying to do is simply read in the contents of the text field on either the button press or the enter key being pressed.

Code:
int
main( int argc, char **argv )
{
    gtk_init( &argc, &argv );

    get_user_name();

    gtk_main();

    return 0;
}

char *
get_user_name()
{
    GtkWidget *dialog;
    GtkWidget *text;

    dialog = gtk_dialog_new_with_buttons( "Name Entry",
                                           NULL,
                                           GTK_DIALOG_MODAL,
                                           GTK_STOCK_OK,
                                           GTK_RESPONSE_ACCEPT,
                                           NULL );
    text   = gtk_entry_new( );

    gtk_container_add( GTK_CONTAINER( GTK_DIALOG( dialog )->vbox ), text );
    g_signal_connect( G_OBJECT(dialog),
                      "response",
                      G_CALLBACK(text_callback),
                      text );


    gtk_widget_show_all( dialog );

    return NULL;
}

void
text_callback( GtkWidget* w, gpointer data )
{
    printf( "%s\n", gtk_entry_get_text( x ));
}


Thanks,
Matt


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 25, 2008 5:38 am 
Offline
Familiar Face

Joined: Wed Apr 02, 2008 10:53 pm
Posts: 28
Hi Matt


From the GTK API doc:

once you created your gtk_dialog with:

Code:
dialog = gtk_dialog_new_with_buttons( "Name Entry",
                                           NULL,
                                           GTK_DIALOG_MODAL,
                                           GTK_STOCK_OK,
                                           GTK_RESPONSE_ACCEPT,
                                           NULL );
text   = gtk_entry_new( );

gtk_container_add( GTK_CONTAINER( GTK_DIALOG( dialog )->vbox ), text );


do:

Code:
gint result = gtk_dialog_run (GTK_DIALOG (dialog));
switch (result)
    {
      case GTK_RESPONSE_ACCEPT:
         do_application_specific_something (text);
         break;
      default:
         do_nothing_since_dialog_was_cancelled ();
         break;
    }
gtk_widget_destroy (dialog);


You can define the do_application_specific_something like:
Code:
do_application_specific_something (GtkEntry    *  text){
printf( "%s\n", gtk_entry_get_text( text ));
}


I believe the advantage of the gtk_dialog is that you don't have to bother with the callback.


Top
 Profile  
 
 Post subject: Re: Callback Confusion
PostPosted: Fri Apr 25, 2008 10:16 am 
Offline
Familiar Face

Joined: Thu Apr 10, 2008 1:43 pm
Posts: 19
k_rock923 wrote:

Code:
void
text_callback( GtkWidget* w, gpointer data )
{
    printf( "%s\n", gtk_entry_get_text( x ));
}



Also I think you have an error inside the callback function.
You must cast gpointer data to the widget type that you're expecting. Example:
printf("%s\n", gtk_entry_get_text( GTK_TEXT( data ) ) );


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 25, 2008 11:15 am 
Offline

Joined: Fri Apr 25, 2008 4:31 am
Posts: 4
Hi. Thanks for the quick reply.

Ok, so I need to typecast first in the callback function. However, it seems that the typecast is exactly what's causing the crash. I'm sort of wondering what's causing that.

gtk_dialog_run seems like a much cleaner idea. However, how can I have it also respond to the user pressing enter on the text entry.

I would really like to understand what I'm doing wrong with the callback, though.

Thanks,
Matt

Code:
char *
get_user_name()
{
    GtkWidget *dialog;
    GtkWidget *text;

    dialog = gtk_dialog_new_with_buttons( "Name Entry",
                                           NULL,
                                           GTK_DIALOG_MODAL,
                                           GTK_STOCK_OK,
                                           GTK_RESPONSE_ACCEPT,
                                           NULL );
    text   = gtk_entry_new( );

    gtk_container_add( GTK_CONTAINER( GTK_DIALOG( dialog )->vbox ), text );
    g_signal_connect( G_OBJECT(dialog),
                      "response",
                      G_CALLBACK(text_callback),
                      text );


    gtk_widget_show_all( dialog );

    return NULL;
}

void
text_callback( GtkWidget* w, gpointer data )
{
    printf( "%s\n", gtk_entry_get_text( GTK_ENTRY( data ) ));
}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 26, 2008 11:54 am 
Offline

Joined: Fri Apr 25, 2008 4:31 am
Posts: 4
I figured it out. My prototype for the signal handler was wrong. d'oh!

Thanks for everyone's help :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group