GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Callback Confusion

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
k_rock923



Joined: 25 Apr 2008
Posts: 4

PostPosted: Fri Apr 25, 2008 4:37 am    Post subject: Callback Confusion Reply with quote

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: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

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
Back to top
cofcof
Familiar Face


Joined: 02 Apr 2008
Posts: 13

PostPosted: Fri Apr 25, 2008 5:38 am    Post subject: Reply with quote

Hi Matt


From the GTK API doc:

once you created your gtk_dialog with:

Code: (C)
1
2
3
4
5
6
7
8
9
10
11

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: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13

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: (C)
1
2
3
4
5

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.
Back to top
clinisbut
Familiar Face


Joined: 10 Apr 2008
Posts: 19

PostPosted: Fri Apr 25, 2008 10:16 am    Post subject: Re: Callback Confusion Reply with quote

k_rock923 wrote:


Code: (C)
1
2
3
4
5
6
7

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 ) ) );
Back to top
k_rock923



Joined: 25 Apr 2008
Posts: 4

PostPosted: Fri Apr 25, 2008 11:15 am    Post subject: Reply with quote

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: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

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 ) ));
}
Back to top
k_rock923



Joined: 25 Apr 2008
Posts: 4

PostPosted: Sat Apr 26, 2008 11:54 am    Post subject: Reply with quote

I figured it out. My prototype for the signal handler was wrong. d'oh!

Thanks for everyone's help :)
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP