GTK (GIMP Toolkit) is a graphical user programming interface. It is completely free of charge, so there is no need to spend anything to develop free or commercial software. Now many Linux integrated systems have packaged GTK1.2. Including RedHat Linux 6.0 and later versions, as well as Turbo Linux in Chinese culture. It is more and more widely used in UNIX system programming.
Another component is Glib, which contains some new extensions of standard applications to improve the compatibility of GTK. Some functions used in Linux systems may not be suitable for standard UNIX systems, such as g_strerror () functions. Some functions also extend the general functions of GNUC. For example, the g_malloc function has its own enhanced debugging functions.
GTK can be bound to multiple languages, including C ++, Guile, Perl, Python, Ton, Ada95, Objective C, Free Pascal, and Eiffel. Compile the software using a program developed in Standard C. You can use GNU with the GTK option. To develop Xwindow Graphics User Programs in other languages than standard C, you need to first refer to the content of the software binding HTTP: // www.gtk.org ). If you use the C ++ language to call GTK for development, you can use the software GTK, which has been bound to C ++, to provide a better C ++ compiling environment than GTK.
The enhanced GTK + version of GTK has been developed. GTK + is a development kit that integrates GTK, GDK, and GLIB. It can work on many UNIX-like systems without GTK platform restrictions.
GTK Message Processing Mechanism
Let's take a look at a basic example. This example generates a window of 200x200 pixels. It can only use shell to kill a process and call the kill command ).
/* Example base. c */
# Include
Int main (int argc, char * argv []) {
GtkWidget * window;
Gtk_init (& argc, & argv);/* initialize the display environment */
Window = gtk_window_new (GTK_WINDOW_TOPLEVEL);/* Create a new window */
Gtk_widget_show (window);/* display window */
Gtk_main ();/* enter sleep status and wait for the event to be activated */
Return (0 );
}
As can be seen from the above program, GTK is an event-driven toolkit. When it runs to the gtk_main () function, it will automatically sleep until an event occurs, and the control is transferred to the corresponding function call, in this function, you can use standard C to write the corresponding transaction logic. This is the same as program processing on windows.
GTK also generates message signals for events on window objects, such as pressing the mouse and activating the keyboard. In this case, the programmer needs to create a signal processor to capture the signal and tell the GTK program which callback function to call after an event occurs. The function for creating a signal processor is defined as follows:
Gint gtk_signal_connect (GtkObject * object, gchar * name, GtkSignalFunc callback_func, gpointer func_data );
The returned value is an associated tag that distinguishes events in the same object from different callback functions. In this way, an object's signal can have any number of callback functions, and each of them will be executed in the declared order. The first parameter of a function call is the widget component that generates signals, that is, the window component such as buttons), and name is the name of the signal or event to be captured, callback_func is the name of the callback function called after the event occurs, and the fourth parameter func_data is the parameter passed to the callback function.
The callback functions must be defined before the main program. Their General formats are as follows:
Void callback_func (GtkWidget * widget, gpointer func_data );
Call the following method to disconnect the callback function from the event:
Void gtk_signal_disconnect (GtkObject * object, gint id );
The second parameter of this function is the return value of the gtk_signal_connect function, that is, the associated tag. The first parameter indicates the name of the object to be joined. In this way, the association between the event and the callback function can be disconnected, so that after the event occurs, the relevant callback function will not be called.
Layout format
Packing Box
The control of GTK display format is usually completed by packing the box. Widget components can be packaged in two ways: horizontal box and vertical box. If the widget component is packaged into a parallel box, the component is inserted horizontally in sequence. If the widget is a vertical box, the components are arranged vertically. The function for generating a new horizontal box is
GtkWidget * gtk_hbox_new (gint homogeneous, gint spacing );
The homogeneous parameter is used to control whether each component in the box has the same size. For example, controls in the horizontal box have the same width and controls in the vertical box have the same height ). The Spacing parameter is the interval between components. The vertical box creation function is gtk_vbox_new). The definition is consistent with that of the horizontal box. The gtk_box_pack_start () and gtk_box_pack_end () functions are used to put packaged objects into these boxes.
Void gtk_box_pack_start (GtkBox * box, GtkWidget * child, gint expand, gint fill, gint padding );
The first parameter is the box pointer you typed in the component, and the second parameter is the component pointer you are going to input. The Expand parameter is used to control whether to allow the component to Expand to the size allocated to the box space and select TRUE), or whether the box size is reduced to the component, so the election is FALSE ). The fill parameter in the function is used to control whether to allocate extra space to the component, that is, to extend the component to the size of the box and select TRUE), or the extra space remains unchanged, reserve as the interval between the box and the packaging component. This parameter is valid only when the expand parameter is set to TRUE. The Padding parameter refers to the interval between the four sides of the component and the box.
Note that when fill is set to FALSE and expand is set to TRUE, the fill value is invalid. The former box is still the size specified when the original box was created, while the latter box has been reduced to the size of the packaging component.
The parameters of the gtk_box_pack_end () function are the same as described above. Only the order is arranged from bottom to top, from right to left.
Finally, pack all the boxes or components into a large box and use the gtk_container_add () function to add the box to the window.
Table Packaging
We can generate a table and place widgets one by one. Widgets occupy all the space allocated to them. To create a table, use the following functions:
GtkWidget * gtk_table_new (gint rows, gint columns, gint homogeneous );
The first parameter is the number of rows in the table. The following parameter indicates the number of columns in the table. The homogeneous parameter is used to arrange the table interval. If this parameter is set to TRUE, the size of each cell in the table is set based on the size of the largest component in the table. The size of all cells is the same. If the homogeneous parameter is set to FALSE, the size of each cell uses the height of the highest component in the same row, and the width of the widest component in the same column.
Put a widget component into a table and use the following function:
Void gtk_table_attach (GtkTable * table, GtkWidget * child, gint left_attach,
Gint right_attach, gint top_attach, gint bottom_attach,
Gint xoptions, gint yoptions, gint xpadding, gint ypadding );
The left_attach parameter and the right_attach parameter indicate where the component is placed and how many boxes are used. If you want to add a button to the lower right cell of the table with two rows and two columns, and want the button to fill the cell, you can select left_attach = 1, right_attach = 2, top_attach = 1, bottom_attach = 2. In fact, left_attach means that the Left Border of the cell where the component is located is the number of edges in the table, and other push methods follow this rule.
The xoptions and yoptions parameters are used to determine the packaging options. You can use OR to select multiple options.
Regulator
GTK has many components that can be adjusted with the mouse or keyboard, such as the Range Widget ). Some components are adjustable throughout the data area, such as Text Widget and Viewport Widget ).
Obviously, the program must be able to process the changes produced by the adjustable components. One solution is to allow the adjustable component to transmit the signal processor when releasing its own signal. Alternatively, you can use another solution to place the adjusted data value in a data structure where the program accesses the structure to obtain the changed parameter value. Sometimes you may need to associate the adjustment of several adjustable components. Adjusting one may also lead to another change. The most obvious example is the processing of the scroll bar and text edit box components. If these associated components have their own methods for processing and adjusting data, the programmer must write a signal processor to convert the adjustment data of one component into the adjustment data of another component, call the adjustment function to set the value.
GTK successfully solved this problem by calling the regulator. The regulator is not a component, but a storage and transfer adjustment data structure. The most typical Regulator Application is to store the values of configuration parameters and range components. The difference is that the regulator also inherits from the Object, which has many characteristics different from the data structure. Most importantly, it also releases signals, which can be captured by the program to respond to user adjustments and edits, and transparently transmitted and adjusted data in the adjustable components.
Generally, the regulator automatically creates components when creating them. For example, let the text component and the scroll bar component use the same regulator as follows:
Text = gtk_text_new (NULL, NULL );
/* Use the newly created regulator for the vertical scroll bar */
Vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)-> vadj );
The regulator inherits from the Object. So it can generate signals like other component objects. When several components share a regulator, they are associated with a signal processor. This signal processor is used to process the "value_changed" signal, which is the same as the signal processed in the program. The following is the definition of the signal in the GtkAdjustmentClass structure:
Void (* value_changed) (GtkAdjustment * adjustment );
When one regulator is used for different adjustable components, any adjustment or change of one component will generate this signal. There are two possible causes for this phenomenon. The first case is that you can adjust the component by using the mouse or keyboard, such as pulling the scroll bar), or directly use the gtk_adjustment_set_value () function in the program to change the value in the regulator.
When the upper and lower parameters of the regulator are reconfigured, the regulator will release a "changed" signal after you add more text to a text edit box. It is defined as follows:
Void (* changed) (GtkAdjustment * adjustment );
The range component associates the signal with a signal processor and reflects the parameter changes on the panel at any time. For example, the size of the slide key in the scroll bar is inversely proportional to the difference between the upper and lower values in the regulator. Once the former changes, the display on the panel also changes accordingly.
You do not need to associate a signal processor with the signal in the program. Everything is done by GTK. If you directly set these parameters of the regulator, you need to call the following statement in the program to release the signal:
Gtk_signal_emit_by_name (GTK_OBJECT (adjustment), "changed ");
Create component Summary
You can follow these steps to create a widget:
Gtk _ * _ new ()-call a very useful function to generate a new widget component.
4.2 Use the gtk_box_pack_start function to connect all signals and events, and generate the corresponding event processor to call the callback function.
4.3 set widget components and regulator features.
4.4 use appropriate functions to package widgets into a container box or table, for example, the gtk_box_container_start () function or the gtk_container_add () function.
4.5 Use the gtk_widget_show () function to display components.
The preceding method can be used to create any window component required by the programmer. After the container is pushed into the window and the window is displayed, the program enters the main cyclic sleep state and the preparation of the main program ends. The event processing logic is compiled in the callback function.
Compile the program
Run the following command:
Gcc my_prog.c-o my_prog.o-lgtk-lgdk
Then, run the my_prog.o program in the Xwindow environment.
At present, X Window Xwindow and GNU compiling system have become the most important Graphics User Interface System on computer workstations and large computers used in linux or unix operating systems, and are also widely used in computers. GTK is a programming development kit that combines the two. It is simpler and more convenient than the Xwindow/Motif programming in the past, with powerful functions and good application prospects. Currently, many free-to-release software developed using this software package have been available on the Internet, greatly enriching the applications on the Linux platform.