Reputation: 643
I am a beginner in gtk and C gui programming and have two specific questions while imlementing a GUI for my C application.
I have called a function via a callback in my code and the function is supposed to return a value. How to get the value in my main from the function?
What is the best way to implement many function calls either from main or nested function calls in GUI using GTK. Should i design and open new window at each function call and if that is the case then how do i close it when its job is over or do i have some way wherein i can change the content of the same window across function calls after some I/O by the functions.
Sample code where i want function hello to return a value 144 after called by callback in main.
#include <gtk/gtk.h>
static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
g_print ("delete event occurred\n");
return FALSE;
}
int hello(GtkWidget *widget, gpointer data)
{
int a=144;
return a;
}
/* Another callback */
static void destroy( GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();
}
int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *button;
int value;
gtk_init (&argc, &argv);
/* create a new window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "delete-event",
G_CALLBACK (delete_event), NULL);
g_signal_connect (window, "destroy",
G_CALLBACK (destroy), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
button = gtk_button_new_with_label ("Hello World");
g_signal_connect (button, "clicked",
G_CALLBACK (hello), NULL);
// printf("value is %d",value);
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (button);
gtk_widget_show (window);
gtk_main ();
return 0;
}
Upvotes: 0
Views: 6806
Reputation: 11454
In your main:
int value = 0;
g_signal_connect (button, "clicked", G_CALLBACK (hello), &value);
You also must respect a signal's callback prototype. In your code, you made it return an int... For the GtkButton::clicked signal, this is:
void user_function (GtkButton *button, gpointer user_data)
Then, to return your value, just modify the variable from inside the callback:
int hello (GtkWidget *widget, gpointer data)
{
int *a = data;
*a = 144;
}
If you need to return several values, use a struct instead of an int.
Your second question is very unclear to me, so I can't answer to it.
Upvotes: 2
Reputation: 1373
As others have said, if you need to get data out of a callback then you need to pass in a pointer and set the value at that location, because you can't return.
This makes sense if you think about what is actually happening. When you are setting the callback when you call g_signal_callback() you are not actually calling your callback, you are just passing a pointer to your function, which GTK is then storing internally for later use.
Once you call gtk_main() you turn control of your program over to the gtk main loop. There, when signals are received the gtk main loop will call into your functions, but you have no control over what is happening inside the gtk main loop, it's not expecting a return value from your function, and if you could return something, you'd have no way of telling gtk what to do with it.
Upvotes: 0
Reputation: 249093
One way to pass values back from a callback would be to give a pointer to some storage to g_signal_connect()
. That pointer will then be passed to your handler, which can write its "return value" there. You could also pass the address of another function (or the address of a struct containing both data and function pointer, or whatever) to enable more complex behaviors in your handler function.
As for your second question, I don't think I understand it, and anyway each question should be its own post here.
Upvotes: 1