 |
GTK+ Forums Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
|
|
|
| Author |
Message |
|
|
TriKri Familiar Face
Joined: 19 Mar 2008 Posts: 19
|
Posted: Thu Mar 20, 2008 5:07 pm Post subject: delete_event vs. destroy? |
|
|
| What's the different between delete_event and destroy for a windows? Also, what is the difference between g_signal_connect and g_signal_connect_swap? Thanks in advance. |
|
| Back to top |
|
 |
dreblen Never Seen the Sunlight
Joined: 14 Jun 2007 Posts: 538 Location: Falun, WI USA
|
|
| Back to top |
|
 |
TriKri Familiar Face
Joined: 19 Mar 2008 Posts: 19
|
Posted: Sat Mar 22, 2008 8:48 pm Post subject: |
|
|
| Thanks! But I didn't understand what they ment with their explanaition of g_signal_connect_swapped(). By the way, I found the delete event in their tutorial: http://library.gnome.org/devel/gtk-tutorial/stable/c39.html. It seems that the delete_event is something that is triggered by the user, while the destroy_event is triggered by something the program itself has done, like trying to destroy the window from within the program, or allowing the program to shut down from within the handel of delete_event. |
|
| Back to top |
|
 |
dreblen Never Seen the Sunlight
Joined: 14 Jun 2007 Posts: 538 Location: Falun, WI USA
|
|
| Back to top |
|
 |
Micah Carrick Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 505 Location: Portland, OR USA
|
Posted: Mon Mar 24, 2008 1:26 pm Post subject: |
|
|
The swapped function allows you to swap the order of the arguments that are passed to the callback function. For example, if you have a GtkButton which will destroy the GtkWindow you would attach a callback function to the "clicked" signal. The callback for the "clicked" callback function is defined in the manual as:
| Code: (C) | 1 2
| void user_function (GtkButton *button,
gpointer user_data) |
This tells us that when the GtkButton is clicked, the callback function we connect will recieve the GtkButton object first, and our user_data second. This is what would happen if we used g_signal_connect ().
However, if we want to attach a callback function in which the FIRST argument is the user_data, then we use g_signal_connect_swapped (). This allows us to use some already written functions as callbacks such as gtk_widget_destroy ().
Example 1: Without using swapped
| Code: (C) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| void my_destroy_callback (GtkButton *button, gpointer user_data)
{
/* destroy the window */
gtk_widget_destroy (GTK_WIDGET (user_data));
}
int main()
{
GtkWidget *button;
GtkWidget *window;
/* create and pack widgets here ... */
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (my_destroy_callback), (gpointer)window);
gtk_main();
return 0;
} |
Example 2: Swapping Arguments
| Code: (C) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14
| int main()
{
GtkWidget *button;
GtkWidget *window;
/* create and pack widgets here ... */
g_signal_connect_swapped (G_OBJECT (button), "clicked",
G_CALLBACK (gtk_widget_destroy), (gpointer)window);
gtk_main();
return 0;
} |
As for the delete-event and destroy events, they have different uses. The delete event occurs when the GtkObject is destroyed. You might use this to cleanup other objects. The delete-event occurs when the GtkWindow is requested to be deleted (user clicks the 'x' for example) and you can stop the delete event from propagating based on your return value. This is often used to prompt people to save their work or confirm they really want to quit. Check out this example: Confirm Quit Dialog on "delete-event" signal |
|
| Back to top |
|
 |
cadcrazy
Joined: 12 Jun 2008 Posts: 2
|
Posted: Fri Jun 13, 2008 1:08 pm Post subject: |
|
|
Thanks Micah Carrick for such a nice explanation.
I have one confusion. What benefit we will get if we pass user data first to library functions like gtk_widget_destroy() through g_signal_connect_swapped() over usual g_signal_connect() call to same function( as i guess we can also call gtk_widget_destroy() throgh g_signal_connect() )
Sorry for noobish question :D |
|
| Back to top |
|
 |
dreblen Never Seen the Sunlight
Joined: 14 Jun 2007 Posts: 538 Location: Falun, WI USA
|
Posted: Fri Jun 13, 2008 2:57 pm Post subject: |
|
|
if you use g_signal_connect to connect to something like gtk_widget_destroy,
it will pass the signal object to the function, whereas _swapped will pass your data to the function
ex:
| Code: (C) | 1 2 3 4
| /* this will delete 'button' on its clicked signal, even though we're passing 'other_widget' as user_data */
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(gtk_widget_destroy), (gpointer)other_widget);
/* this will delete 'other_widget' on button's clicked signal */
g_signal_connect_swapped(G_OBJECT(button), "clicked", G_CALLBACK(gtk_widget_destroy), (gpointer)other_widget); | |
|
| Back to top |
|
 |
|
Powered by phpBB © 2001, 2005 phpBB Group CodeBB 1.0 Beta 2
|