GTK + Learning: overview, Build Environment (Windows,linux), development __linux

Source: Internet
Author: User
Tags event listener function definition gtk

GTK + Learning: Overview


Compared to Swing's GUI construction in java,c/c++ environment, it is much more complicated. The first is that C + + language does not have an official GUI library. As a result, the third party class library has sprung up. Because C + + does not have Java similar cross-platform, so most of its class libraries are limited platform, such as Microsoft's MFC. Of course, there will be a lot of cross-platform GUI libraries, such as QT, such as wxwidget, such as the GTK + we are going to explain here.

Relative to MFC, QT, and wxwidget,gtk+ using the object-oriented framework of the pure C language. This is a small but functionally not inferior GUI class library. The GNOME environment under Linux is written in GTK + and is similar to Photoshop, and is inextricably linked to GTK +. Here is a brief introduction to GTK +.

Speaking of GTK +, first of all, GIMP. In many articles that recommend using free software instead of commercial software, most of them mention the use of open source gimp instead of Photoshop. GIMP is an acronym for the GNU Image Manipulation Program, originally an image processor on a Unix-like operating system, and is now ported to the Windows platform. To simplify the development of GIMP, GIMP Toolkit was born, and this is GTK. After adding the object-oriented feature, it adds a plus sign after its name, and becomes GTK +.

GTK + is a library that creates graphical user interfaces that can run on Unix-like platforms, Windows platforms, and other devices. GTK + relies on several of the libraries listed below:

* GLib-a common tool library, not just for creating graphical user interfaces. GLib defines a lot of data types, macros, file tools, etc.
* Pango-internationalized text binding;
* ATK-Provides a common interface for graphical user interface interactive access technology;
* Gdkpixbuf-Allows the creation of pixel buffers from image data or image files;
* GDK-The abstraction layer provided for the different window systems, providing an abstraction between the local graphics interface and GTK +, is platform-dependent. If you need to migrate GTK + on other platforms, you only need to rewrite gdk;
* gtk-gtk+ Library, providing a variety of controls.

We learn about GTK + only to use GTK +, not too much entangled in the use of these libraries.


GTK + Learning: Setting up the environment


After you have learned about GTK +, the next task is to build your environment so that you can learn from GTK +. The environment needs to be built into Windows and Linux two platforms:

Windows platform:
In [Url]http://www.gtk.org/download.html[/url] can find a version of Windows (32-bit) and 64-bit download, according to their system is 32-bit or 64-bit Select the appropriate version, the page of GTK + Individual packages is a necessary library for GTK + operations, third party dependencies is a third-party dependent library. This allows us to download the corresponding library files as needed to minimize the GTK + library. If you want to be simple, there is also a bundle package on the page that packs all the libraries together to download the usage. Note that the bundle package is not listed, but is the link in the body of the page, you may need to look for it carefully.

Here we download the bundle package, after decompression, you can add the Bin folder inside the environment variable path, so that we compile the exe file runtime can find those DLLs. Otherwise, these DLLs need to be copied to the same directory as the EXE. You can also copy all of these DLLs in the bin to the C:/windows/system32 folder, because even if you add them to the system environment variable, you can't find the DLL in the IDE, copy it to the System32 folder.

The following uses VC6 to configure the compilation environment.

First, select Include files in the directories below VC6 tools-options, add the Include folder in the GTK + directory and all of the first-level subdirectories inside, and then add the Include folder in the subdirectory under the Lib folder. In this case, add 10 directories altogether:

Then add the Lib folder in the GTK + directory to the library files:

After that, the VC6 has been configured, and the VS2008 and the like are configured as well. Create a new console project (VC6) or an empty project (VS2008), open the project settings, clear the original object/library modules under the Link tab, and 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 a few, OK. Here is a vc6,vs2008 with similar settings.

After this setting, you can create a new main.c file, and then enter:

#include <gtk/gtk.h>

int main (int argc, char** argv)
{
gtkwidget* window;
Gtk_init (&AMP;ARGC, &AMP;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;
}

Compile, link, run, there is a window to show the success of the environment configuration:

Linux Platform:
First of all, I am using the Linux version is Ubuntu, desktop environment is gnome. As I said before, Gnome is written with GTK +, so the runtime environment does not need to be configured, but to install the environment required for development.

Just installed Ubuntu has already installed GCC by default, but does not install the required header files, we need to manually add:
sudo apt-get install build-essential

And then install the GNOME Development Kit:
sudo apt-get install Gnome-core-devel

The system automatically finds the libraries it relies on and downloads them together.

After this installation, use the following command to compile the above code:
GCC main.c-o main ' pkg-config--cflags--libs gtk+-2.0

Note that ' here is the inverted quotation mark on the left side of the keyboard 1, not single quotes.

It's not clear how the KDE desktop is configured with GTK +, but it may be similar, but you need to install GTK + 's running environment first.

Above is the environment configuration of GTK + under Windows and Linux platforms. These steps have been tested and passed on my machine. After configuring the environment, we will start to learn new.


GTK + Learning: Not just hello world

In general, when learning a new technology, what I would most like to see is not a thick book of theoretical books or programming skills, even if there is no skill, I hope to be able to see my results, even if only a small window can not do anything to satisfy my curiosity. So, let's start by writing a small program to see how the GTK + program is written.

Since we've configured the compilation environment last time, just open your favorite IDE or Notepad and knock down the following code. Here I compile using VS2005 under the Windows platform, some terms may be different under other platforms, but the overall is similar.

#include <gtk/gtk.h>

int main (int argc, char** argv)
{
gtkwidget* window;
Gtk_init (&AMP;ARGC, &AMP;ARGV);
window = Gtk_window_new (gtk_window_toplevel);
Gtk_widget_show (window);
Gtk_main ();
return 0;
}

Then compile the connection, and after the run, if there is no error, a small window will appear. Well, that's the interface we've written with GTK +.

Now let's look at this procedure. The first line first, is the reference header file, generally as long as the reference to this gtk.h. The introduction of the include file in the previous method is the gtk.h in the GTK directory.

Then the main function definition, which is exactly the same as the C language.

In the main function, the first sentence declares a gtkwidget pointer. As I said before, GTK + is designed in accordance with object-oriented thinking, you might as well take this gtkwidget as a class, although the C compiler does not think so. Then the Gtk_init call, which is initializing the GTK + environment. A friend who has written an OpenGL program should be more aware that OpenGL has a similar init function in it. Then assign a value to the pointer that was previously declared. Look at this function name: Gtk_window_new, it is clear that a new window pointer is created. According to object-oriented writing is:
gtkwidget* window = new Gtkwindow (gtk_window_toplevel);

What do you think. It's a lot clearer. In fact, it has been hinted that Gtkwindow inherited the Gtkwidget class. As you can see later, Gtkwidget is actually the parent class for all the controls. The passed parameter is Gtk_window_toplevel, indicating that it is a top-level window. Then use the Gtk_widget_show setting to display it, as well as the object-oriented syntax:
Window-> Show ();

The last sentence, Gtk_main, brings our program into GTK + 's event listening loop.

In this way, our program is finished. But maybe there's a problem: How about a dark console window behind it? This is because the default mode of operation is debug, with release to see. The ugly window is gone. And the resulting binaries are a lot smaller than that.

Well, when we press the Close button, the window is gone, but the program doesn't quit. Yes, because we didn't add the event listener, so when we closed the window, GTK + didn't know what to do. So, we continue to modify the code as follows:

#include <gtk/gtk.h>

int main (int argc, char** argv)
{
gtkwidget* window;
Gtk_init (&AMP;ARGC, &AMP;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;
}

Notice that we have added an event listener function:
G_signal_connect (Gtk_object (window), "Destroy", G_callback (Gtk_main_quit), NULL);

It means that for object window, when the "destroy" time occurs, the Gtk_main_quit function is invoked, and the arguments passed to the function are null. If we use the Java language Event listener notation, this is the case:
Window.adddestroylistener (New Callbackfunc () {
Gtk_main_quit ();
});

But this code should also be relatively easy to read. In fact, this is GTK + add event listening to the writing, each control of each event listening to write code, very unified. After the modification is completed, click the Close button: Ha, the program exits.

So, let's revise the program:

#include <gtk/gtk.h>

int main (int argc, char** argv)
{
gtkwidget* window;
Gtk_init (&AMP;ARGC, &AMP;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 a look. The window title has been replaced by the classic Hello world!.

Read the code and we added the phrase:
Gtk_window_set_title (Gtk_window (window), "Hello world!");

If you can think of its object-oriented syntax, it achieves its purpose.
((gtkwindow*) window)-> settitle ("Hello world!");

Note that because the C compiler does not understand object-oriented polymorphic mechanisms, GTK + uses a number of macros for type conversions, such as the Gtk_window here. Remember that we're declaring a gtkwidget pointer that needs to be converted into a gtkwindow pointer to use the Set_title function. At the same time, this macro also has the function of type detection, if cannot convert, will throw an exception.

Well, now that the step-by-step code has been added, we've learned about the process of writing the GTK + program, and can create a form that adds events to monitor and modify control properties--gui programming is not the subject of this. The rest is the details, and the learning of the vast library of controls.

Related Article

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.