Principle of signal and callback functions
At version 2.0, the signal system has been moved from GTK to GLIB, so there is a prefix "G_" instead of "gtk_" in the description of the function and type. We don't plan to introduce the details of the Glib 2.0 signal system relative to GTK 1.2 signal system extension.
We will discuss the signal and callback function before we analyze the HelloWorld program in detail. GTK is an event-driven kit that means it will wait for GTK_MAIN () until the next event occurs, the control is transmitted to the appropriate function.
Controlling is a "signal" approach. (Note that the signal here is not equivalent to the signal in the UNIX system, and it is not intended to be implemented, although they!! Use the words.) When an event occurs, if you press the mouse button, the components pressed A suitable signal will be issued. This is the working mechanism of GTK. Signals with all components, such as "Destroy", with component proprietary signals, such as the "Toggled" signal emitted by the toggle button.
To make a button execute an action, we need to set the connection between the signal and signal processing functions. You can use the function to use the function:
Gulong G_Signal_Connect (GPOINTER * Object, Const Gchar * Name, gcallback func, gpointer func_data;
The first parameter is the component to be issued. The second parameter is the name of the signal you want to connect, the third parameter is the function you want to call when the signal is captured, and the fourth parameter is the function you want to pass. The data.
The third parameter specified is called a callback function:
Void Callback_func (gtkwidget * widget, gpointer caverback_data);
The first parameter is a pointer to the component of the signal, and the second parameter is a pointer to the data, which is the last parameter of the g_signal_connect () function.
Note that the declaration of the above callback function is a common form, and some of the special signals of some components use different call parameters.
Another call is used in the form of HelloWorld programs, which is:
Gulong g_signal_connect_swapped (gpointer * Object, const gchar * name, gcallback func, gpoint * slot_object);
g_signal_connect_swapped () and g_signal_connect () are the same, but the callback function uses only one parameter, a pointer to the GTK object. So when using this function to connect the signal, the callback function should be in the form.
Void Callback_Func (gtkobject * object);
This object is often a component. However, we usually do not use the function g_signal_connect_swapped () to set up. They are often used as parameters in a callback function that only accepts a separate component or object, as in our HelloWorld example.