Design GLTK + graphics interface using Glade and Libglade

zhaozj2021-02-17  83

Zhang Wei, Cao Jian

GLADE has been developed under GNOME, which is an extension of the traditional interface design tool GTK / GDK that meets the basic needs of the graphical user interface based on GTK library. I. Introduction to Glade If there is a software that can put the design of the graphical interface in front of the developer, and after designing, you can directly see the appearance effect of the interface, so that the programmer's main energy is concentrated in the application core. The development of functions is the so-called visual programming idea. In a Windows environment, this visual integrated development environment has many mature products such as VC , VB, PB, Delphi, C Bulider, etc. The software based on the Linux operating system is not rich, but it is not a blank, such as the Kylix launched by Borland. At present, GLADE is developed under GNOME, which is an extension of the traditional interface design tool GTK / GDK to meet the basic needs of graphical user interface based on GTK libraries. Glade can generate interface code for you, the basic process is as follows: 1. Let Glade design interface elements, set interface element properties, set up callback functions for the signal; 2. Exclude it as XML format, observe and modify does not meet the requirements Part; 3. Write the other part of the code and load the Glade file in the application, compile the C code generation execution file. This mode allows the design interface to completely separate from other part of the code, and it is possible to make it without having to compile the application after the interface is changed. The Graphical User Interface Design Group does not have to care for the application's operational details, while programmers can use their time for other aspects, without repeating a large number of cumbersome GTK functions for the modification of the interface. This form of division of labor meets the requirements of software engineering, and it is more convenient to development for GUI software. Second, using Glade's preparation conditions Before learning to use Glade, we assume that you have an initial understanding of the basic use of the Linux operating system, familiar with the processing command of the RPM package or Gzip package, and understand the C language, and familiarize yourself with message processing. Programming mechanism. The above basis is a must-have knowledge using Glade. If the reader is not unknown in some of the above, you can check the relevant information. In order to more flexibly develop applications, users can be familiar with the GCC compiler to master the writing rules of the Make file. Let's take a look at how to add Glade to the development environment of the Linux operating system, mainly divided into two steps: 1. Get the source code of Glade You can find the source code of Glade in most Linux operating systems, or from the Internet Download, it is currently convenient to get the source code website is www.glade.pn.org, downloaded files may be RPM or ZIP format. 2. Decompression, compile and install Glade after obtaining the source code, and then decompress the compressed package, and then run Make Compile Glade. Third, the design interface After a preliminary understanding of Glade, let's take a look at its true face, run XWindow on Linux, call up the graphical interface, you can enter Glade on the virtual console or The glade item under the developer menu is found on the command menu, click to click. After taking the Glade, we can generally see three windows (Figure 1): An engineering management window (main window), a common component window and an attribute management window. These three windows constitute a GUI development environment, and these three windows should be familiar for users who use visual programming. The engineering management window is mainly responsible for the management of an engineering project, and what we said is to refer to the design of the graphical interface.

The most used in this window is its menu, and the common menu is to create a new project, open an existing project file to edit modification, and change the perspective of the project. Figure 1 The usual component window is a graphical set of common GTK components provided by Glade. When you need to call a component, you only need to click the icon of the component in this window, and then click the mouse in the design, then the component is arranged to the appropriate location. The attribute window is mainly responsible for adjustment of the properties of the project object. It itself is a paginous Notebook component, which focuses on item object (component) attributes, such as the title name, size, location, and docking Alignment, shortcut key settings, and signal processing, etc. It is worth mentioning that some components are root components, or the top container components (such as top-level window components, dialog members, etc.), each of the top container components, in the main area of ​​the engineering management window, the top container is Independent, to make them can generate a small number of editing by the source code generated by Glade. All of the top members of a top-level member are maintained in a component tree by Glade, we can call the "View Components Tree" in the Project Management window to select a single item tree. Fourth, generating code options When we design the desired interface, let Glade generate the code we need, click the build button, Glade will pop up an option window, three pages in this window, the ordinary options, C language options, Libglade option (Figure 2). Figure 2 5. The main directory and code files generated by Glade generated files and code files are as follows: There are three subdirectories / Po, / SRC and / Macros under the root of the project (assuming / root / projects / project1). The source code is placed in the / src directory, and the files in this directory can be modified by the text editor to increase the application. 6. Use the XML pointer to establish a component libglade to construct a component according to the XML document (.glade file) when the program is run, in order to apply libglade, we initialize GNOME and libglade in the beginning of the program.

The initial procedure is as follows: /  Initialization GNOME  / gnome_init ("Phonebook", Version, Argc, Argv); /  Initialization libglade  / glade_gnome_init (); obtaining a component pointer code as follows: /  Declaration Pointer  / Gladexml XML; GTKWIDGET DIALOG1; /  Found the root manager named "About1" in the XML interface file generated by the Glade software, generate an XML component and assigns the component to XML / XML = Glade_xml_new ("Phonebook.glade", "About1" ); Dialog = glade_xml_get_widget (XML, "Dialog1"); /  Take timely release  / gtk_object_unref (gtk_object (xml)); Design interface often has a relatively independent component, in the construction of application projects The components are established, such as the parent-child relationship of the main window and dialog, etc., the steps are as follows: 1. Built two components with XML: Dialog = glade_xml_get_widget (XML, "Dialog1"); app = glade_xml_get_widget (XML, "App1") ; 2. Use gnome_dialog_set_parent (gtk_dialog (dialog) function to set the dialog as the slave component of the APP window. Note: A little needed to note when using the Glade design interface, all component names cannot be renamed because we have to access the components in their name. 7. Message Processing Functions For message processing functions, we can specify (as shown in Figure 3) when designing components (Figure 3), and Glade automatically adds a function name in the generated code. If you use libglade, there are two points: 1. The parameters received by the return function cannot specify the type and can only use the empty pointer type GPOINTER (VOID); Figure 3 2. Because libglade is called the back call function when it is running, so this Class functions cannot be set to Static type. Eight, instance code The following is an example program generated by Glade, it is a simple phone thin program, a total of three independent components, one main window, an entered dialog, and a CLIST for displaying the entry. List components. We can click the button to pop up the dialog from the main window. There are two entry bar in the dialog box, which are used to enter the name and phone number. In the dialog, the dialog is closed. Interested readers can be rich in this application.

#include #include #include #define version "1.0" static gtkwidget APP; static gtkwidget clist; void do_nothing (gtkwidget W); void run_about_dialog GtkWidget w); void add_number (GtkWidget w); void do_nothing (GtkWidget w) {gnome_app_message (GNOME_APP (app), "Does not do anything!");} void run_about_dialog (GtkWidget w) {GladeXML xml ; xml = Glade_xml_new ( "phonebook.Glade", "about1"); if (! xml) {g_warning ( "We could not load the interface!"); return;} gtk_object_unref (GTK_OBJECT (xml));} static void really_add_number (GtkWidget w, int button, gpointer data) {char strings [2]; GtkWidget name_entry; GtkWidget phone_entry; GladeXML xml = data; if (button = 0!) return; name_entry = Glade_xml_get_widget (xml, "add_name_entry "); Phone_entry = glade_xml_get_widget (XML," Add_Phone_Entry); strings [0] = gtk_entry_get_text (gtk_entry (name_entry)); strings [1] = gtk_entry_get_text (gtk_ent " ry)); gtk_clist_append (GTK_CLIST (clist), strings);} void add_number (GtkWidget w) {GtkWidget dialog; GladeXML xml; xml = Glade_xml_new ( "phonebook.Glade", "dialog1"); if (xml! ) {g_warning ( "We could not load the interface!"); return;} dialog = Glade_xml_get_widget (xml, "dialog1"); gnome_dialog_set_parent (GNOME_DIALOG (dialog), GTK_WINDOW (app)); gtk_signal_connect_full (GTK_OBJECT (dialog), " clicked ", GTK_SIGNAL_FUNC (really add_number), NULL, xml, (GtkDestroyNotify) gtk_object_unref, FALSE, FALSE);} int main (int argc, char argv []) {GladeXML xml; gnome_init (" phonebook "

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

New Post(0)