Si Jie Wang Jinglin Zhou Dongfang (China People's Liberation Army Information Engineering University)
【Abstract】 This article introduces the XWindow graphics window programming tool GTK under the Linux platform, and gives the basic elements and steps of using GTK programming.
[Keywords] GTK, callback function, message processor, regulator GTK (GIMP Toolkit) is a graphical user programming interface tool. It registers completely free, so it is used to develop free software or business software. Many Linux integration systems have now been packaged in GTK1.2 version. Including Redhat Linux 6.0 or higher, as well as Chinese Turbo Linux, etc. It is also increasingly being applied to UNIX system programming. There is also a component called GLIB, which contains new extensions for some standard applications to increase the compatibility of GTK. Some functions for Linux systems may not be suitable for standard UNIX systems, such as g_strerror () functions, and more. Some functions have also extended the general function of GNUC, such as the G_Malloc function, has its own enhanced debugging. GTK can be bound to a variety of languages, including C , Guile, Perl, Python, Ton, ADA95, Objective C, Free Pascal, Eiffel. With the program developed by standard C, compile software can use the GNU and come with the GTK option. If you want to use other languages other than standard C to develop xwindow graphics user programs, you need to first refer to the contents of the binding software (http: // www.gtk.org). If you use the C language to call GTK for development, you can use the software that has been bound to the C to GTK - software to provide a better C compilation environment than GTK. The enhanced GTK of GTK has been developed. GTK is a development kit that integrates GTK, GDK, GLIB, which works on many systems similar to UNIX, without GTK platform restrictions. 1. Let's take a basic example below to see a basic example of a 200 × 200 pixel window. It can't quit it yourself, can only kill the process by shell (call the kill command). / * Example Base.c * / #include
The first parameter of the function call is the Widget component (ie, window components such as the button), and the Name is the name of the signal or event you want to capture. Callback_func is the callback function name called after the event occurs, and the fourth A parameter FUNC_DATA is the parameter passed to the callback function. The callback function is defined in front of the main program, and their general format is as follows: void callback_func (gtkwidget * widget, gpointer func_data); call the following method will allow you to disconnect the callback function to the event: void gtk_signal_disconnect GTKOBJECT * Object, GINT ID); the second parameter of this function is the return value of the above GTK_SIGNAL_CONNECT () function, ie the associated label. The first parameter pointing to the removal of the associated object name. This makes it possible to disconnect the event and the callback function, so that the related callback function will not be called after the event occurs. Layout format
2.1 Packing box Control for GTK display format is usually done by a packing box. Widget components can be packaged in two ways, horizontal boxes and vertical boxes. If the Widget component is packaged into the parallel box, the component is inserted horizontally; if it is a vertical box, the component arrangement is vertical. The function of generating a new horizontal box is gtkwidget * gtk_hbox_new (gint homogeneous, gint spacing); parameter homogeneous is used to control each component in the box has the same size (for example, the control in the horizontal box has the same width, vertical The controls in the box have the same height). The spacing parameter is the interval between components. The creation function of the vertical box is gtk_vbox_new (), the definition is consistent with the horizontal box. GTK_BOX_PACK_START () and gtk_box_pack_end () functions are used to put packaging objects into these boxes. Void gtk_box_pack_start (gtkbox * box, gtkwidget * child, gint, gint, gint, gint padding); the first parameter is the box pointer you want to play in, the second parameter is the component pointer you will want to play. The expand parameter is used to control whether the components are allowed to extend to the size of the space (selected true), or the size of the box is contracted to the component (select false). The Fill parameter in the function is used to control whether multiple spaces are assigned to components, which is to extend to the size of the box (selected true), or excess space constant, retaining the interval between the box and the package assembly. This parameter is only valid when the expand parameter takestrue TRUE. The Padding parameter refers to the interval size of the component and the box. Note that Fill takes the false value, and the expand takes the true value and the FALSE value is taken, the Fill value is invalid. The former's box is still the size specified when the box is created, while the latter's box has shrunk to the size of the package component. The parameter of the gtk_box_pack_end () function is consistent with the above description. It is only from bottom to right to left. Finally, all boxes or components are packaged into a large box and add the box to the window with the gtk_container_add () function. 2.2 Form Pack We can generate a table that puts the Widget component one by one. The Widget component will occupy all the space assigned to it. Creating a table is the following function: gtkwidget * gtk_table_new (gint rows, gint columns, gint homogeneous); the first parameter, obvious, is the number of rows of the table. The following parameters are the columns of the table. The Homogeneous parameter is used to schedule a table interval size. If it takes True, the size of each small size in the table is set to the size of the largest component in the table. If the Homogeneous parameter takes False, each size size is used in the height of the highest component in the peers, the width of the widest component in the same column. A widget component into a table, with 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 will point out where the component is placed, and how many boxes have been used.
If you want to add a button in the lower right in the table in two lines, and want the button full of little, the parameters can choose LEFT_ATTACH = 1, right_attach = 2, top_attach = 1, Bottom_attach = 2. In fact, Left_attach is also the left frame of the component where the component is the number of the number of the table, and the other can be pushed. Parameters XOPTIONS and YOTIONS are used to determine the package option, you can use or to select multiple options.
The regulator GTK has many components to be adjusted with a mouse or keyboard, such as a range component (RANGE WIDGET). There are also some components to be adjusted, such as the text widget, and viewport widgets throughout the data area. Obviously, the program is to process changes to the adjustable components. One solution is to adjust the adjustable component to transfer the data value to the signal processor when it releases its own signal. Or use another solution to place the adjustment data value into a data structure, and access the structure by the program to obtain a changed parameter value. Sometimes you may need to associate several adjustable components, adjusting a change in another. The most obvious example is the processing of scroll bars and text editing block components. If these associated components have a method of processing adjustment data, the programmer must write a signal processor to convert the adjustment data of a component into another component, and call the adjustment setting function to set this value. Go in. GTK calls the success of the regulator successfully. The regulator is not a component, but stores and transmits the structure of the adjustment data. The most typical adjuster application is the value stored in the configuration parameters and range components. Different, the adjuster is also inherited from the object (Object), which has many features that are different from the data structure. Most importantly, it also releases signals, but these signals can not only be captured by programs to respond to user adjustments and editing, but also adjust data in the adjustable components. The general regulator creates automatically when the component will create a component. For example, let the text components and scroll components use the same regulator as follows: Text = gtk_text_new (null, null); / * Use the adjuster to be created for vertical scroll bar * / vscrollbar = gtk_vscrollbar_new (gtk_text (text) -> VADJ); the regulator is inherited from the object Object. Therefore, it can generate signals as other components 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, and the signal is the same in the program. Here is the definition of this signal in the GTKADJUSTMENTCLASS structure: void (* value_changed) (gtkadjustment * Adjustment); Different adjustable components When using a regulator, any component is adjusted to generate this signal. There are two cases that cause this phenomenon. The first case is that the user changes the value value in the regulator in the program with a mouse or keyboard to adjust the component (for example, pulling roll) or directly in the program. When the UPPER parameter and the Lower parameter of the regulator are reconfigured, the conditioner releases the "ChangeD" signal when the user needs to add more text to a text edit box. It defines the following: Void (* Changed) (gtkadjustment * Adjustment); range components associate this signal with a signal processor and reflects changes in the panel at any time. For example, the size of the sliding keys in the scroll bar is inversely proportional to the Upper, the LOWER value in the regulator. Once the former has any changes, the display on the panel also changes accordingly. You don't need to associate a signal processor in the program, everything is done by GTK. If you 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 From above, create a Widget component Complete with the following steps:
GTK _ * _ new () - calls a very useful function to generate a new Widget component. 4.2 Connect all signals and events with a gtk_box_pack_start () function, generate the corresponding event processor to call the callback function. 4.3 Set the feature of the Widget component and the regulator. 4.4 Packaging the Widget components into a container (box or table) with a suitable function, such as a gtk_box_container_start () function or gtk_container_add () function, and more. 4.5 Display components with a gtk_widget_show () function. With the above method, you can create any window components needed by the programmer, and then enter the window and display the window, the program enters the main loop sleep state, and the main program is completed. The processing logic of the event is placed in the callback function. The compiler uses the following command: gcc my_prog.c -o my_prog.o -lgtk -lgdk, run the My_Prog.o program in the xwindow environment. Current X Windows and GNU Compile Systems have become the main graphical user interface system for computer workstations and large computers, which apply Linux or UNIX operating systems, and widely used in the microcomputer. GTK is the programming development package combined with both. It is more simple and convenient than the XWindow / Motif programming, and has a good application prospect. There are many free distribution software developed by this package, greatly enriched the application of Linux platform. [References] 1. "GTK TURTOIL" Peter Mattis, Spencer Kimball, Josh Macdonald http://www.gtk.org 2. "Linux System Administration Guide" Mfkomarinski, C. Collet X Xia Martin Translation 1999, Tsinghua University Press 3. "Unix Network Programming" W.Richard Stevens 1998, Tsinghua University Press