1、 g_signal_connect and g_signal_connect_swap
函数形式:
g_signal_connect( gpointer *object,
const gchar *name,
GCallback func,
gpointer userdata );
g_signal_connect_swapped( gpointer *object,
const gchar *name,
GCallback func,
gpointer *slot_object );
The swapped function allows you to swap the order of the arguments that are passed to the callback function.
(g_signal_connect_swapped允许你交换传递给回调函数的参数的次序.)
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:
void (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
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
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;
}
2、delete_event and destroy
The difference between "delete_event" and "destroy" is, as far as i understood from reading the official documentation, is that the "delete_event" is an EVENT, and the "destroy" is a SIGNAL.
Events are issued from the Operating System (more precisely, from the Window engine), and Signals are issued from the widgets themselves. So when a window is being closed (the user pressed the "x" button or the system asks for closing the program), the Operating System issues the "delete_event". Then, if the process of this event is not handled by a callback, or the callback that handles the event returns false (0), then a "destroy" signal is issued for the window.
Typical pattern: The user attempts to close the window. The system asks the user "Are you sure you want to quit?" Y / N
In this case, you can handle the situation by implementing the callback function connected to the "delete_event", in which you will ask the question. If the user says Yes, you return false: the "destroy" signal will be issued and then the window closes. If the user says No, you return true: the "destroy" signal will not be issued, and the window won't close.
You may (should) also implement a callback attached to the "destroy" signal, in which you call for the gtk_main_quit() function in order to quit the gtk event loop properly.
大意:delete_event是一个EVENT,而destroy是一个SIGNAL。EVENT发自操作系统,SIGNAL则产生于组件本身。当你关闭一个窗口时,操作系统发出一个delete_event,如果确认关闭(Y),则会发出destroy信号。
更多信息可点击:http://developer.gnome.org/gobject/stable/gobject-Signals.html#g-signal-connect
http://www.gtkforums.com/viewtopic.php?t=1177