Hello World @ GTK + 2.0 Chinese tutorial serialization

Source: Internet
Author: User
Tags gtk
Hello World

Now that we know the basic theory, let's analyze it in detail.Helloworld
Sample program.

This is the callback function to be called when a button is clicked. In this example, we ignore the parameters widget and data, but it is not difficult to use these parameters. The next example uses the data parameter to tell us which button is pressed.

Void Hello (gtkwidget * widget,
Gpointer data)
{
G_print ("Hello World/N ");
}

The next Callback Function is a bit special. "Delete_event" occurs when the window manager sends this event to the application. Here we can choose what to do for these events. You can ignore them and make a little response or simply exit the program.

The value returned by this callback function allows GTK to know how to do it. Returns true to let it know that we do not want the "Destroy" signal to be sent, so that the program continues to run. If false is returned, the "Destroy" signal is sent, which then calls the "Destroy" signal processing function.

Gint delete_event (gtkwidget * widget,
Gdkevent * event,
Gpointer data)
{
G_print ("delete event occurred/N ");

Return true;
}

Here is another callback function, which calls gtk_main_quit () to exit the program. This function tells GTK to exit from gtk_main when the control is returned to it.

Void destroy (gtkwidget * widget,
Gpointer data)
{
Gtk_main_quit ();
}

I suppose you know the main () function... yes, like other programs, all GTK programs have a main () function.

Int main (INT argc,
Char * argv [])
{

Next, we declare two pointers to the structure of the gtkwidget type. They are used to create a window and a button.

Gtkwidget * window;
Gtkwidget * button;

Gtk_init () again (). As before, this initialization toolkit analyzes the parameters in the command line. It deletes any identifiable parameters from the parameter list and modifies argc and argv to make the deleted parameters as if they never existed, allowing your program to analyze the remaining parameters.

Gtk_init (& argc, & argv );

Create a new window. This is intuitive. It allocates memory for the gtkwidget * window structure, so that the window now points to a valid structure. It creates a new window, but this window will not be displayed until you call gtk_widget_show (window) in the later part of the program.

Window = gtk_window_new (gtk_window_toplevel );

This
There are two examples connecting a signal processing function to an object (in this example, window. Here, "delete_event" and "Destroy"
The signal is captured. When we use the window manager to close the window or call the function gtk_widget_destroy (),
When a component is transmitted to it as an object for destruction, the "delete_event" signal is sent. When we return false in the "delete_event" signal processing function
Value, the "Destroy" signal is sent.G_object
AndG_callback
It is a macro that performs type conversion and detection for us, and also increases the readability of the Code.

G_signal_connect (g_object (window), "delete_event ",
G_callback (delete_event), null );
G_signal_connect (g_object (window), "Destroy ",
G_callback (destroy), null );

Next, this function is used to set the attributes of the container object. Set the Border width to 10 pixels. In
Set component attributes
This chapter also contains other similar functions.

Again,Gtk_container
It is also a macro that executes type conversion.

Gtk_container_set_border_width (gtk_container (window), 10 );

This function calls to create a new button. Allocate space in the memory to a new gtkwidget structure, initialize it, and point the button pointer to it. It is displayed with a "hello World" label.

Button = gtk_button_new_with_label ("Hello World ");

In
In this case, let's make this button useful. We set a signal processing function for the button, so when the button sends a "clicked" signal, the hello () function is called. We ignored
The data parameter, which simply transmits null to the hello () callback function. Obviously, when we click the button with the mouse, the signal "clicked" is sent.

G_signal_connect (g_object (button), "clicked ",
G_callback (Hello), null );

Me
You also need to use this button to exitProgram. This will demonstrate how the "Destroy" signal is triggered by the window manager or by our program. When we press the button, it first calls
Hello () callback function, and then this function, which depends on the order in which they are connected. You can have many callback functions, and all the Callbacks are executed in the order you set the connection. Because
The gtk_widget_destroy () function only accepts the gtkwidget * widget as the unique parameter.
The g_signal_connect_swapped () function replaces the Orthodox g_signal_connect ().

G_signal_connect_swapped (g_object (button), "clicked ",
G_callback (gtk_widget_destroy ),
G_object (window ));

This is an assembly call.
This chapter will be explained in depth. However, it is quite easy to understand. It simply tells GTK to put the button in the window, that is, where it is displayed. Note that a GTK container can only contain one component. There are other components. Later, we will introduce the design to deploy multiple components in various ways.

Gtk_container_add (gtk_container (window), button );

Everything is ready. After all the signal processing functions are connected, the buttons are also placed in the window. Let GTK "display" these components on the screen. The window component is displayed at the end, so that the entire window will pop up, instead of seeing the window pop up first and then seeing the button again. In this simple example, you will not notice it.

Gtk_widget_show (button );

Gtk_widget_show (window );

Then, of course, we call the gtk_main () function to wait for events from the X server. When these events arrive, the calling component sends a signal.

Gtk_main ();

Finally, after the function gtk_quit () is called, the control is returned here.

Return 0;

Now
At, when we click a GTK button with the mouse, the component sends a "clicked"
Signal. In order for us to use this information, the Program sets a signal processor to capture that signal, which calls the function in sequence according to our choice. In our example, when the button is pressed
Call the hello () function as a parameter, and then call the next processing function of the signal. This function calls gtk_widget_destroy ()
Function to pass the window component as a parameter and destroy the window component. This causes the window to send a "Destroy" signal, which is captured and calls our destroy ()
Callback Function to exit GTK.

If the window manager is used to close the window, it will trigger "delete_event ". This will call our
"Delete_event" handler. If we return true in the function, the window remains there and nothing happens. If false is returned, GTK will be sent
"Destroy" signal, of course, it will call the "Destroy" callback to exit GTK.

Home next >>
event up continue

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.