GTK programming in Borland C Builderx for Windows
Xinzhen@auvtech.com by Xinzhen (Lonestar)
BCBX
After experiencing a series of failed "cross-platform" efforts, the independent tool software manufacturer Borland finally hoped in C Builderx, this industrial-grade heavy-duty C development environment. C Builder is significantly different from previous BC or BCB. It is not only the IDE written by Java, but is more essentially characterized by its flagship positioning, which finally got the long-awaited characteristics: the characteristics of the word "C ": high standard compatible grammar, open , Compatible, independent of the platform.
GTK
GTK is a cross-platform toolbox for building a user interface. It is said that it is a user interface, more accurate descriptions should be a "framework", integrated all the underlying architectures required to develop a graphical interface program, such as signal transfer mechanism, asynchronous I / O, character set and coding processing, etc. The current stability is 2.2.4. As the function continues to increase, its application is increasingly wide. Compared to other business grade application frameworks, this library uses extremely loose and commercially friendly LGPL licenses, making it more and more attention.
GTK for Windows
However, although there is a layer of encapsulation layers in the GTK architecture to maximize the platform-independent, it comes from the nature of the UNIX world, which makes it run in Windows, still requires a user to build a complex environment to compile and set up set. Fortunately, half "official" Windows transplant release has appeared, located at http://www.dropline.net/gtk/index.php. From this website, you can download it to compiled GTK for Windows Runtime Environment and Development Environment (corresponding to Linux GTK2 and GTK2-DEVEL Pack), and how to use this library in VC, even how to make installation Introduction to the program.
The released release from this station has built-in a topic engine WIMP. When this theme engine is running in Windows XP, the Native XP Topics API is called, obtain information such as color schemes, fonts, and maps, gaining a Look and Feel that is completely consistent with XP. Of course, if it is not XP, you can only draw controls in an imitation, but at least color is still consistent.
BCBX with GTK
When BCBX 1.0 is released at the end of last year, one is long as industrial grade products, and the "graphical interface" is small, in short, it does not bring any elements used to assist graphical user interface (GUI) development. .
The openness of BCBX is reflected in 3 platforms, and 5 different compilers. Intel and Microsoft compilers are listed as support. The most unexpected thing is that the GNU's GCC is also in the list, and will automatically load your system with the installer, without any additional download, installation steps, you can use:
Its version is shown as "GNU C Version 3.2 (Mingw Special 20020817-1) (MingW32)"
This feature can not help but let us create a joint. This means countless, GNU or non-GNU, Open's Sources will have a simple way to use it on Windows. GTK is of course one of them. Configure GTK for Windows Environment
We now install the above GTK Windows release. This release has a beautiful installer that looks from the standard Windows application. The real Cross-Platform, you have to be integrated into the platform where you have the same appearance and behavioral characteristics as other applications, making it a Native application. The kind of self-contained, the program is incompetent with the environment, cannot be called the platform, but it is a platform and can only run on his own platform.
First download Runtime Environment from here. Current version 2.2.4.1.
After loading, the dynamic library in the form of a DLL is loaded into C: / Program Files / Common files / gtk / 2.0 / lib. In order to let the system find it, we need to set a $ PATH environment variable for it. The setting method on Windows 2000 is like this. Press the combination of WIN PAUSE, if you don't have a 104-key Windows keyboard (rare animal), you have to right click on "My Computer" (it's actually you, not mine). Select an Advance tab, click the Environment Variables ... button. Look in the PATH variable in the dialog, no, create a new one. Edit its content to join the above path.
Ok, now we download and install development environment. In this example, I put it to D: / gtk2dev, and the settings later will be subject to this. You can change your own discretion.
After being installed, test whether it is normal. Go to the directory D: /GTK2DEV/SHARE/GTK-2.0/Demo, run gtk-demo.exe. If you see an interface, then install successfully.
Configuring a BCBX environment
GTK for Windows site has a very detailed instruction to compile and run on Windows. But they are too complicated, and I have to describe Step by STEP by new construction in BCBX.
Step1:
We will create a new project in BCBX, select New Console.
Don't worry, I didn't make a mistake. Although what we have to develop is indeed Gui, we are not new gui Application, otherwise you have to find Argc and Argv trouble. I named the project gtkhello.
STEP2:
Now, in order to compile this project, first, we teach the compiler to find GTK header files. Right-click on the left corner project name and select "Build Options Explorer ...". In the upper part of the dialog appearance, "MingW GNU C Compiler", in the lower Paths and Defines tab, add the following strings in the include Path box:
D: /gtk2dev/include/gtk-2.0; d: /gtk2dev/lib/gtk-2.0/includ; d: /gtk2dev/include/glib-2.0;
D: /GTK2DEV/LIB/glib-2.0/include; d: /gtk2dev/include/pango-1.0; d: /gtk2dev/include/atk-1.0;
D: / gtk2dev / include
STEP3:
Teach our linker how to link calls to GTK programs. In the same dialog, the upper part selects "GNU C Linker", and the library search path box will appear in the underlying tab. Input D: / GTK2DEV / LIB
The lib static interface library corresponding to each DLL is stored in this directory. Then, we transfer to the Options page, behind the Linker Options-> Search for library libname, enter the following strings:
GLIB-2.0; GTK-Win32-2.0; GDK-Win32-2.0; GOBJECT-2.0; GTHREAD-2.0; GMODULE-2.0; PANGO-1.0
These are the names of all interface libraries. At this point, all settings are completed. We open it on the IBM DeveloperWorks website before 2 years.
Gnomenclature: Prepare for GNOME 2, Part 1
"One article, put the" Listing 2 "program into the Untitled.cpp we have just generated, click Make from the toolbar. The compilation process should be completed soon.
Stop! Bitfields and Align Align
When the CPU accesses the memory, it always reads and writes in its entirety. For example, the X86 CPU always reads 4-byte data from the integer multiple of 4 bytes, which cannot be read from any length data from any position as you want. For efficiency, by default, the compiler always allows the integer to store the integer multiple addresses of its length. In a structure, in order to do this, sometimes you have to waste a few bytes.
For example, we define a structure:
Struct {char C; int 1;
From the literal, this structure has a length of 5 bytes, but by default, the compiler always assigns 8 bytes, which is to let i appear on the offset 4 address.
Bit domain Bitfields is a member in the C language structure, which can specify the number of bits of the number of memory occupied by the member. However, in the alignment of the bit domain, the two compilers of GCC and MSVC have produced huge differences. Now, we will change the structure above to the following:
Struct {char C;
INT B: 1; INT I;
We inserted an integer that only 1 bit memory in C and I. In GCC, we test the length of this structure, and found that it is still 8 bytes, that is, B utilizes the voids between C and I, without more space. However, in VC we will find that the structure is 12 bytes. That is to say, the B is aligned on the 4x addresses like all other integers.
If you just do this, unfortunately, if you are inserting a bit domain B2, the length is still 12. And if inserted into a SHORT type, the length will become 16!
The reason is that VC uses a quirky alignment and has no complete document description. Basically, VC integrates the same data structure bit field in the structure, and then each bit domain group is default to align by its data type. There are also many different exceptions. This is different from the alignment mode supported by any normal GCC. What does aligned different mean? Considering that Windows is compiled with VC, that is, all Windows APIs use VC alignment. And if you compile Windows programs with MingW GCC, you will make an error over all Windows APIs that use the bit domain! And our GTK for Windows is obviously used in this alignment.
Fortunately, the Windows version GCC makes it a new command line switch to compile, -MMS-bitfields makes it use VC compatible alignment. This MINGW GCC carried by Borland is also available. The GCC on this switch is not available. Plus the code generated after this compilation switch will have the same behavioral characteristics as the code of the VC. Just Borland seems that I haven't realized that the GCC in the package has such an important switch. I have to find the hook that there is this option. Fortunately, the openness of BCBX saved us again in this time again. At the bottom of the Options tab, there is an Other Options and Parameters option. The parameters filled in the inside will be copied to the command line. So we write inside:
-MMMS-bitfields
OK, now, it's coming. Press the RUN button, our first GTK program will run:
This is the effect of the PANGO tag, compared to the figure in the original text.
This is a GTK multi-page notepad
At this point, our GTK for Windows development environment has been successful, and the rest only needs Coding.
UTF-8, is UTF-8 everywhere
At the end of this article, we finally look at an example. "GTK 2.0 Programming Example" (published by Tsinghua University Press), Mr. Song Guowei in the application of a HelloWorld program in the article. In this program, there is such a sentence:
g_signal_connect (g_object (button), "clicked",
G_callback (on_button_clicked), (gpoint) "Hello! / N free world.");
The program itself is not wrong, however, in general, after compiling operation, this sentence cannot be seen, the opposite is on the console.
** (gtkhello.exe: 1236): Warning **: invalid utf8 string passed to pango_layout_set_text ()
Such a word. GTK is an international toolbox that stores a string in all parts of its interior, or you can display any language on the same screen. It requires all input strings to be UTF-8 encoding. However, "usually", we have edited Chinese files will be saved with a Locale Encoding, such as GB2312. Therefore, as long as the source file can be saved as UTF-8, it is solved. BCBX has this capability. Right click on the CBX project file and select Properties. Switch to the General tag, set Encoding to UTF8. Only all new files will be saved using the encodings selected here.
We create a new .C file to copy the content in the past. Note that there is a UTF8 word below the file.
Then compile the run.