Getting started with GTK: Signal and callback functions, and gtk callback Functions

Source: Internet
Author: User

Getting started with GTK: Signal and callback functions, and gtk callback Functions

The GTK interface we learned earlier is static. If we press the button, it does not respond. How can we make it respond? Next we will learn about GTK signal and callback functions.


GTK uses a signal and callback function to process events, messages, or signals from outside the window. When a signal occurs, the program automatically calls the callback function of the signal connection.


When learning application programming, we often come into contact with the term "signal. The "signal" in GTK is actually a software interruption. "Interrupt" is often encountered in our lives. For example, I was playing a game in the room and suddenly sent a courier. I gave "interrupt" to the game, I signed for express delivery (Handling interrupted). After the handling, I continued to play my game.The "signal" in GTK is such an "interruption". When a user presses a button, an "interruption" is generated, which is equivalent to a signal, then it will process such an "interrupted task" (in the program, the experience is to call a function ).


The "signal" can be considered as an interruption sign in GTK. For example, if the button is pressed as "pressed", the release button is marked as "released ",These marks are equivalent to the same keywords in C language. We must write them according to their names when using them.It should be noted that the signal signs of each control are not necessarily the same. For example, the buttons (GtkButton) contain the "pressed" signal, and the window (GtkWindow) does not have this signal, the specific signal of each control should be determined by checking the help document.


Common buttons:

"Clicked": triggered when the button is pressed.

"Pressed": triggered when the release button is released.

"Released": triggered when the release button is released.


For programs, we press the button to call a function. If there are functions A, B, and C, how do we determine to call function A only after pressing the button, instead of function B or C. At this time, we need A rule that calls function A after pressing the button, just like A traffic rule, the red light goes green, and the signal registration function is doing this.


Signal registration function:

Gulong g_signal_connect (

Gpointer instance,

Const gchar * detailed_signal,

GCallback c_handler,

Gpointer data );

Instance: Specifies the control that is operated by the signal sender. If you press the button, This is the button pointer.

Detailed_signal: Signal sign, such as "pressed"

C_handler: name of the callback function, which must be converted using G_CALLBACK ().

Data: The parameter passed to the callback function. gpointer is equivalent to the void of C language *

Return Value: Sign of the function to be registered


For example:

G_signal_connect (button,

"Pressed ",

G_CALLBACK (callback ),

NULL );

When the button is pressed, the callback function callback (equivalent to processing the interrupted task) is automatically called. The callback function callback can be any function, and the function name is automatically named as needed, if it is not a library function, we have to define this callback function. It should be noted that the method of writing the callback function (return value, parameter) is not what we want to write, the help document specifies how to write the callback function. If you do not write the callback function as required, unexpected errors may occur.


To use the help documentation, click here.


Callback Function Definition:

Void callback (

GtkButton * button,

Gpointer data)

{

}


Description of callback function parameters:



It is equivalent to passing the first parameter of g_signal_connect () to the first parameter of the callback function, and the last parameter to the last parameter of the callback function.


Next, let's take this example and press the button to print the text on the button to the screen.


Set the spacing between containers and controls:

Void gtk_container_set_border_width (

GtkContainer * container,

Guint border_width );

Container: container

Border_width: the spacing between containers and controls. For example, guint is equivalent to the uint of C language.



Get the text content on the button:

Const gchar * gtk_button_get_label (GtkButton * button );

Button: button

Returned value: the obtained text content.


The complete code is as follows:

# Include <gtk/gtk. h> // header file // The processing function for button pressing. gpointer is equivalent to void * void deal_pressed (GtkButton * button, gpointer user_data) {// button points to the button of the main function // user_data points to the "I am a button" of the main function // obtain the text information of the button const char * text = gtk_button_get_label (button ); printf ("% s =========% s \ n", (char *) user_data, text); // print content} int main (int argc, char * argv []) {gtk_init (& argc, & argv); // initialize GtkWidget * window = gtk_window_new (GTK_WINDOW_TOPLEVEL ); // create a top-level window // set the width of the window border (the gap between the control in the window and the window border is 15) gtk_container_set_border_width (GTK_CONTAINER (window), 15 ); gtkWidget * button = gtk_button_new_with_label ("^_^"); // create the button gtk_container_add (GTK_CONTAINER (window), button); // put the button into a window (window is also a container) /* after the button is pressed (pressed), The deal_pressed () * "I am a button" is automatically called. It is the parameter passed to the callback function deal_pressed () */g_signal_connect (button, "pressed ", g_CALLBACK (deal_pressed), "I am a button"); gtk_widget_show_all (window); // display all controls of the window gtk_main (); // return 0 for the main event loop ;}


Click here to download the source code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.