GTK + learning: Overview, Build Environment (Windows, Linux), development

Source: Internet
Author: User
Tags gtk

GTK + learning: overview


Compared with swing in Java, Gui construction in the C/C ++ environment is much more complicated. First, the C/C ++ language does not have an official GUI library. As a result, third-party class libraries have sprung up. Because C/C ++ does not have cross-platform features similar to Java, most of its class libraries are platform-specific, such as Microsoft's MFC. Of course, there will also be a lot of cross-platform GUI Libraries, such as QT, such as wxwidget, such as GTK + which we will explain here.
 
Compared with MFC, QT, and wxwidget, GTK + is written in pure C language using the object-oriented framework. This is a small but not inferior GUI class library. The GNOME Environment in Linux is compiled using GTK + and is similar to the gimp of Photoshop. It is also closely related to GTK +. Next we will briefly introduce GTK +.
 
To start with GTK +, we must first talk about gimp. In many articles that recommend using free software instead of commercial software, most people will mention that using open-source gimp instead of Photoshop. Gimp is short for GNU Image Manipulation Program. It was originally an image processing program on Unix-like operating systems and has been transplanted to Windows. To simplify gimp development, gimp toolkit was born, which is GTK. After the object-oriented feature is added, a plus sign is added to its name, which becomes GTK +.
 
GTK + is a library for creating graphical user interfaces. It can run on Unix-like platforms, Windows platforms, and other devices. GTK + depends on the following libraries:

* Glib-a common tool library, not only used to create graphical user interfaces. Glib defines many data types, macros, and file tools;
* Pango-international text binding;
* ATK-provides a common interface for interactive access to graphic user interfaces;
* Gdkpixbuf-allows pixel buffering from image data or image files;
* Gdk-provides an abstraction layer between the local graphic interface and GTK + for different window systems and is platform-related. To transplant GTK + to other platforms, you only need to rewrite the gdk;
* GTK-GTK + Library provides various controls.

We learn GTK + only to use GTK +, so we will not be too entangled in the use of these libraries.

GTK + learning: build an environment


After learning about GTK +, the following describes how to build an environment for GTK + learning. The environment can be set up on two platforms: Windows and Linux:
 
Windows:
In [url] http://www.gtk.org/download.html#/url#, you can find the windows (32-bit) and 64-bit versions to download. Select the corresponding version based on whether your system is 32-bit or 64-bit, GTK + individual packages on the page are required for GTK + running, and third party dependencies is a third-party dependent library. In this way, we can download the corresponding library files as needed to minimize the number of GTK + libraries. For simplicity, a bundle package is also provided on the page, which packages all the libraries and can be downloaded for use. Note that the bundle package is not listed separately, but the link provided in the page body. You may need to look for it carefully.
 
Here we download the bundle package. After decompression, we can add the bin folder in it to the environment variable path so that we can find the DLL when compiling the EXE file. Otherwise, copy these DLL files to the same directory as the EXE file. You can also copy all the DLL files in the bin to the C:/Windows/system32 folder, because even if you add these DLL files to the system environment variables, you cannot find the DLL during ide running, copy to the System32 folder.
 
The following uses vc6 to configure the compiling environment.
 
First, select include files under the tools-options directory of vc6, add the include folder under the GTK + directory and all the first-level subdirectories in it, and then add the include folder in the subdirectory under the Lib folder, in this case, a total of 10 directories are added:
 
Then add the Lib folder under the GTK + directory to the library files:
 
In this way, vc6 is configured, and vs2008 is similar. Create a console Project (vc6) or an empty project (vs2008), open the settings of the project, and clear the original object/library modules in the Link tab, then add glib-2.0.lib gtk-win32-2.0.lib gdk-win32-2.0.lib gobject-2.0.lib gdk_pixbuf-2.0.lib gthread-2.0.lib gmodule-2.0.lib intl. lib, OK. Vc6 and vs2008 are used here.
 
After this setting, you can create a new main. c file and enter:
 
# Include <GTK/GTK. h>
 
Int main (INT argc, char ** argv)
{
Gtkwidget * window;
Gtk_init (& argc, & argv );
Window = gtk_window_new (gtk_window_toplevel );
G_signal_connect (gtk_object (window), "Destroy", g_callback (gtk_main_quit), null );
Gtk_widget_show (window );
Gtk_main ();
Return 0;
}
 
Compiling, linking, and running. If a window appears, the environment is configured successfully:
 
Linux platform:
First, declare that I use Ubuntu as the Linux version and gnome as the desktop environment. As mentioned earlier, gnome is written using GTK +. Therefore, the runtime environment does not need to be configured separately. The environment required for installation and development is required.
 
The installed Ubuntu has installed GCC by default, but it does not have the required header file. We need to manually add:
Sudo apt-Get install build-essential
 
Then install the gnome development kit:
Sudo apt-Get install gnome-core-devel
 
The system automatically finds the dependent libraries and downloads them together.
 
After installation, use the following command to compile the above Code:
GCC main. C-o main 'pkg-config -- cflags -- libs GTK ++-2.0
 
Note: 'Here is the backticks on the left side of the keyboard, not single quotes.
 
It is unclear how to configure GTK + on the KDE Desktop. The steps may be similar, but do you need to install the running environment of GTK + first?
 
The above is the GTK + Environment configuration on Windows and Linux platforms. These steps have passed the test on my machine. After the environment is configured, we will start to learn new things.

GTK + learning: not just hello World

When learning a new technology, what I want most is not the thick theoretical book or programming skills. Even if there is no skill, what I want is to see my achievements, even a small window that can't do anything, it can satisfy my curiosity. So now let's write a small program to see how the GTK + program is written.
 
Since the compiling environment has been configured last time, open your favorite ide or notepad and click the following code. Here I use vs2005 for compiling on Windows. Some terms may differ from each other on other platforms, but they are similar in general.
 
# Include <GTK/GTK. h>

Int main (INT argc, char ** argv)
{
Gtkwidget * window;
Gtk_init (& argc, & argv );
Window = gtk_window_new (gtk_window_toplevel );
Gtk_widget_show (window );
Gtk_main ();
Return 0;
}
 
Then compile the connection. After running, if there is no error, a small window will appear. Well, this is the interface we wrote with GTK +.
 
Next let's take a look at this program. The first line is the referenced header file. Generally, you only need to reference this GTK. h. If you introduce the include file according to the method described above, it is the GTK. h In the GTK directory.
 
Then the main function is defined, which is exactly the same as the C language.
 
In the main function, the first sentence declares the pointer of A gtkwidget. As mentioned above, GTK + is designed according to the object-oriented idea. You may consider this gtkwidget as a class, although the C compiler does not think so. Then call gtk_init, which is to initialize the GTK + environment. Friends who have written OpenGL should be clear. OpenGL also has a similar init function. Then assign values to the previously declared pointer. Look at the function name: gtk_window_new. It is clear that a new window pointer is created. The object-oriented method is as follows:
Gtkwidget * window = new gtkwindow (gtk_window_toplevel );
 
How is it? A lot clearer, right? Actually, it is implied that gtkwindow inherits the gtkwidget class. As you can see later, gtkwidget is actually the parent class of all controls. The passed parameter is gtk_window_toplevel, indicating that it is a top-level window. Then use gtk_widget_show to set the display. Similarly, the object-oriented syntax is as follows:
Window-> show ();
 
The last gtk_main sentence brings our program into the event listening loop of GTK +.
 
In this way, our program is completely introduced. But you may find a problem: why is there a black Console window behind it? This is because the default running mode is Debug. Can you change it to release? The ugly window is gone, right? The generated binary file is much smaller than that one.
 
Well, when we press the close button, the window disappears, but the program does not exit? Yes, because we didn't add event listening, so when the window is closed, GTK + does not know how to do it. Therefore, we continue to modify the Code as follows:
 
# Include <GTK/GTK. h>

Int main (INT argc, char ** argv)
{
Gtkwidget * window;
Gtk_init (& argc, & argv );
Window = gtk_window_new (gtk_window_toplevel );
G_signal_connect (gtk_object (window), "Destroy", g_callback (gtk_main_quit), null );
Gtk_widget_show (window );
Gtk_main ();
Return 0;
}
 
Note: We added an event listening function:
G_signal_connect (gtk_object (window), "Destroy", g_callback (gtk_main_quit), null );
 
It means that for the window object, when the "Destroy" time occurs, the gtk_main_quit function is called and the parameter passed to this function is null. If we use the event listening method in Java, it is like this:
Window. adddestroylistener (New callbackfunc (){
Gtk_main_quit ();
});
 
However, this code should be easy to understand. In fact, this is how to add event listening in GTK +. Every event listening of each control is written in this way, which is very unified. After the modification is complete, run it. Click the close button: Ha, the program exits!
 
Then, let's modify the program:
 
# Include <GTK/GTK. h>

Int main (INT argc, char ** argv)
{
Gtkwidget * window;
Gtk_init (& argc, & argv );
Window = gtk_window_new (gtk_window_toplevel );
Gtk_window_set_title (gtk_window (window), "Hello world! ");
G_signal_connect (gtk_object (window), "Destroy", g_callback (gtk_main_quit), null );
Gtk_widget_show (window );
Gtk_main ();
Return 0;
}
 
Run it to see? The title of the window has been changed to the classic Hello world! .
 
After reading the code, we added the following sentence:
Gtk_window_set_title (gtk_window (window), "Hello world! ");
 
If you can think of its object-oriented syntax, you can achieve the goal:
(Gtkwindow *) window)-> settitle ("Hello world! ");
 
Note that the C compiler does not understand the object-oriented polymorphism mechanism. Therefore, GTK + uses many macros for type conversion, for example, gtk_window. Remember that we declare the gtkwidget pointer. You need to convert it to the gtkwindow pointer to use the set_title function. At the same time, this macro also has the type detection function. If it cannot be converted, an exception will be thrown.
 
Now, after step-by-step code addition, we have learned how to write the GTK + program and can create a form, add event listening and modify control properties-isn't the subject of GUI programming? The rest is some details and learning about the huge control library.

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.