GTK + 2.0 Tutorial - Hello World Explanted

zhaozj2021-02-16  177

Hello World

Now we know the basic theory, let us analyze the HelloWorld sample program in detail.

This is the callback function to be called when the button is clicked. The parameters Widget and Data are ignored in the example, but it is not difficult to use these parameters. The next example will use the DATA parameter to tell us which button is pressed.

Void Hello (gtkwidget * widget, gpointer data) {g_print ("Hello WORLD / N");}

The next one of the callback functions is a bit special. When the "delete_event" event occurs, we can choose what to do here, you can ignore them, you can do some responses or simple exit programs.

The value returned by this callback function allows GTK to know how to do it. Returns True, GTK knows that we don't want to issue a "Destroy" signal and keep the program to continue running. Returns False, let's let the "destroy" signal, and then call the "Destroy" signal processing function immediately.

Gint delete_event (gtkwidget * widget, gdkevent * Event, gpointer data) {g_print ("delete evenet occurred / n");

Return True;}

This is another callback function that calls the function gtk_main_quit () to exit the program.

Void Destroy (gtkwidget * widget, gpointer data) {gtk_main_quit ();}

I assume that you know the main () function ... Yes, like other programs, all GTK programs have a main () function.

INT Main (int Argc, char * argv []) {

Next declared two pointers to gtkwidget. They are used to create a window and a button.

GTKWIDGET * Window; gtkwidget * butt;

Here is gtk_init (). Initialize the toolkit, analyze the command line parameters, remove any parameters that can be identified from the parameter list, and modify Argc and Argv. Allow program to analyze the remaining parameters.

GTK_INIT (& Argc, & Argv);

Create a new window. This is very intuitive. It has established a new window, but this window does not display, and then we call the function gtk_widget_show (window) to display it.

WINDOW = gtk_window_new (gtk_window_toplevel);

This has an example of two connection objects and signal processing functions. Here "Delete_Event" and "Destroy" signals were captured. When we use the Window Manager to close the window or call the function gtk_widget_destroy (), the "delete_event" signal is sent. When we return false values ​​in the "DELETE_EVENT" signal processing function, the "Destroy" signal is sent. G_object and g_callback are macros, perform type forced conversion and detection for us, and also increase 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 (design), null; this function is used to set the properties of the container object. Set the window border width of 10 pixels. See the chapter setup component properties for details

GTK_CONTAINER is also a macro execution type Casting.

GTK_CONTAINER_SET_BORDER_WIDTH (GTK_CONTAINER (Window), 10);

This function call creates a new button. The allocation is stored in a new component structure, initializes it, and points the Button pointer to it. The button's label is "Hello World".

Button = gtk_button_new_with_label ("Hello World");

Here, we let this button make some useful things. We set the signal processing function for the button, so when the button issues a "Clicked" signal, the Hello () function is called. We ignore the Data parameters, simply transmit NULL to Hello () callback. Obviously, when we click on the button with the mouse, the signal "ClickED" is sent.

g_signal_connect (g_object (button), "clicked", g_callback (hello), null;

We also want to use this button to exit the program. You will see how to issue a "Destroy" signal from the program. As in the above, when we press the button, first call the Hello () callback function, depending on the order in which we set up the connection. You can have a lot of backup functions, all callbacks are executed in order you set up. Because the gtk_widget_destroy () function only accepts a parameter, we use the function g_signal_connect_swapped () replacement function g_signal_connect ().

g_signal_connect_swapped (g_object (button), "clicked", g_callback, g_object (window);

This is an assembly call, see the chapter of the assembly component. It is quite easy to understand. It is simple to tell GTK to put the button to the place to display. Note that the GTK container can only contain one component. There are other components, in later, multiple components can be placed in a variety of methods.

GTK_CONTAINER_ADD (GTK_CONTAINER (Window), button;

everything's ready. We let GTK "display" on the screen. The window component is finally displayed, so the entire window will pop up, instead of seeing the button first, see the button. Although we will not notice in this example.

GTK_WIDGET_SHOW (Button);

GTK_WIDGET_SHOW (Window);

We call the gtk_main () function to wait for events from the X server, when these events come, call the component to signal.

gtk_main ();

Call the function gtk_quit () after control returning here.

Return 0;

Now, when we click on a GTK button with a mouse, the component issues a "Clicked" signal. We set the handler for this signal. In our example, when the button is pressed, the function hello () is called, and then the next processing function of the signal is called, the function call function gtk_widget_destroy (), passes the window component as a parameter to it, destroy the window component. Cause the window issues a "destroy" signal and call our Destroy () callback function, simply exit GTK. If you use the Window Manager to close the window, it will trigger the "delete_event" event, which calls our "delete_event" process function. If we return True in the function, what is going on, return false, will trigger the "destroy" signal, call the "Destroy" callback function, exit GTK.

转载请注明原文地址:https://www.9cbs.com/read-8482.html

New Post(0)