Let's program 1 with gtk (START and warm-up) (repost some gtk articles)

Source: Internet
Author: User
Tags cairo gtk
Thanks to the author.
^

Program 1 with gtk (START and warm-up)

Reprinted please indicate the source: http://lvjinhua.cublog.cn
Author: lvjinhua at gmail dot com
2006.09.20

  • Preface

The purpose of this article is to provide a learning file for beginners who are new to GTK programming. You can input and compile all the code by yourself, in order to achieve the purpose of learning. At the same time, I also hope it will become a reference manual. I hope every API in GTK can find corresponding examples in this article.

I will try my best to ensure that all the code is the shortest and small, so that everyone can have enough motivation to manually enter it.

Due to my limited time and energy, I cannot present all the content to all readers at a time. Therefore, I have drawn up a plan to update the content of a chapter every day and perform serialization on my Blog, at the same time will constantly modify and improve the previous content, so in order to ensure that everyone can read the latest content, please pay attention to the author's Blog: http://lvjinhua.cublog.cn

Start Date: 2006.09.20

  • Why is GTK?

Why not GTK? I don't want to spend too much saliva to tell why I chose GTK instead of other things. No technology is good or bad. Only people who use it have the ability to do so; I think it is wise to compile more programs and make less arguments.

  • 1. Introduction to GTK

GTK (GIMP Toolkit) is a Toolkit used to create a graphical user interface. It complies with the LGPL license, so it can be used to develop open source software, free software, or even commercial software with closed source code, without any cost to purchase licenses and use rights. Currently, GTK has been successfully applied to a large number of free software and commercial software, and has achieved great success.

The creator of GTK:
Peter Mattis: petm@xcf.berkeley.edu
Spencer Kimball: spencer@xcf.berkeley.edu
Josh MacDonald: jmacd@xcf.berkeley.edu

Currently, GTK mainly consists of several components:
* Gtk +: the main component of GTK, including all graphical controls and a large number of practical APIs.
* Glib: contains some replacement functions of standard functions and some functions that process data structures such as linked lists. These substitution functions are used to enhance the portability of GTK and provide enhanced versions of the corresponding libc functions.
* Pango: this component is used to process international text output.

  • 2. Preparation

If you want to work for xxx, you must first sharpen the tool! Before actual program design, you should first specify some major development tools.
1) GTK is cross-platform and can run on Linux/Unix/Windows/MacOS and embedded systems, but this is not the focus of this article. In fact, as long as you program based on the APIS provided by GTK, programs designed on one platform can be compiled successfully on other platforms without any modifications.

The operating system used by the author is: Dubuntu-6.06, all the sample code in this article will be developed and compiled on the platform to run; of course, this does not mean that other operating systems can not be used, after installing the GCC compiling environment, GTK, Glib, and Libc libraries, you can basically compile and run all the examples in this article, the following describes how to install the GTK development environment of Ubuntu Dapper:

Sudo apt-get install vim # Yes, I use vim to write code. Of course, you can use any editor you like.
Sudo apt-get install build-essential # This will install
Gcc/g ++/gdb/make and other basic programming tools
Sudo apt-get install gnome-core-devel # This will install libgtk2.0-dev libglib2.0-dev and other development-related library files
Sudo apt-get install pkg-config # used to automatically find the header file and library file location when compiling the GTK Program
Sudo apt-get install devhelp # This will install the devhelp GTK document viewing program
Sudo apt-get install libglib2.0-doc libgtk2.0-doc
# This will install the gtk/glib API reference manual and other help documents
Sudo apt-get instal glade libglade2-dev # This will install GTK-based interface Constructor

Of course, for other Linux distributions, you can refer to the relevant documents to build the development environment. The major Linux forums are a good place to consult: www.linuxsir.org, www.linuxfans.org

If you are enthusiastic about how to install the GTK Development Library of other operating systems, you are welcome to provide it to me. I will share it with you here! Thank you first.

In Windows, MinGW is used as an example to set the environment:

Blog from linxh 1. From http://www.gimp.org /~ Download the following tml/gimp/win32/downloads.html file:

  • Glib-2.12.1.zip. Runtime for GLib 2.12.1. Requires libiconv and gettext.

  • Glib-dev-2.12.1.zip. Corresponding developer package, I. e. headers, import libraries and documentation.

  • Gtk0000-2.8.20.zip. runtime for GTK + 2.8.20. requires GLib 2.12, atk 1.10, Pango 1.12 and cairo 1.2. in addition, if you want to be able to load PNG, JPEG or TIFF images with gdk-pixbuf, you will need libpng and zlib, libjpeg and libtiff respectively.

  • Gtk0000-dev-2.8.20.zip. Corresponding developer package.

  • Pango-1.12.3.zip. Pango 1.12.3. Pango is used by GTK + 2 and GIMP 2. Requires GLib 2.12 and cairo 1.2.

  • Pango-dev-1.12.3.zip. Corresponding developer package.

  • Atk-1.10.3.zip. Atk is used by GTK + 2.

  • Atk-dev-1.10.3.zip. Corresponding developer package.

  • Cairo-1.2.2.zip. Cairo is used by GTK + 2 and Pango.

  • Cairo-dev-1.2.2.zip. Corresponding developer package.

 

  • Pkg-config-0.20.zip. PKG-config is a neat program that is useful in makefiles etc and extensively used in building software according to the GTK + and gnome conventions, using autotools etc. it uses a "Database" specifying interdependencies among softawre packages. it is used to get the compile and link flags needed when building software using libraries that provide PKG-config data. requires glib 2. x.

  • GNU libiconv for win32. both runtime and developer files. GNU libiconv is an implementation of iconv. Glib uses libiconv, so you will need this.

  • Gettext-0.14.5.zip. Just the GNU libintl (internationalization Library) DLL (intl. dll). Glib uses libintl, so you will need this.

  • Gettext-dev-0.14.5.zip. GNU gettext deveappsfiles and tools for Win32, containing the header libintl. h, msgfmt.exe, other programs, documentation, and varous other files. libintl. h is needed if you build software that uses GNU gettext for internationalization. msgfmt is needed if you want to recompile message catalogs. the other commands can be used to manipulate message catalogs in other ways.

    Decompress the package to a directory (take C:/GTK as an example ). 2. Slave. 3. compile :( 1) set the environment variable set pkg_config_path = C:/GTK/lib/pkgconfig (2) Run PKG-config -- cflags -- libs GTK +-win32-2.0 (3) add the above output as a parameter during compilation
    • 3. Hello, Dubuntu!

    Well, we have been waiting for a long time. Let's start our first program!

    Main functions of this program: display a window, and display the string "Hello, Dubuntu!" in the title bar of the window! "
    The source code is as follows. After compilation is successful, you can run this program by entering./hello_dubuntu through the command line. The result is as follows:

    Hello_dubuntu.c
    # Include <gtk/gtk. h>
    /* The header file above can be found in the/usr/include/gtk-2.0.
    * The header file above is used to include all the headers that may be used for GTK programming.
    * Files, including glib. h
    */
    Int
    Main (int argc, char * argv [])
    {
    GtkWidget * window;
    // GtkWidget is the base class of most visual components.
    Gtk_init (& argc, & argv );
    // Standardize the command line parameters passed in by the program
    Window = gtk_window_new (GTK_WINDOW_TOPLEVEL );
    // Create a window (200x200)
    Gtk_window_set_title (GTK_WINDOW (window), "Hello, Dubuntu! ");
    // Set the window title
    Gtk_widget_show (window );
    // Display window
    Gtk_main ();
    // Main loop of the Gtk Program
    Return 0;
    }
    Compile the program: gcc hello_dubuntu.c-o hello_dubuntu 'pkg-config -- cflags -- libs gtk +-2.0 '# Note: "'" is not a regular single quotation mark "'", but is the same "~ "The symbol that comes together!

    Yes, the above program is very simple. There are only a few statements except comments! However, it is indeed a GTK program. Although it is not perfect yet, it lacks signal processing. Therefore, when you click "close" to close this window, the program is still running.

    1) Compile the command line of the program:
    The above Code uses the gnu c language compiler gcc, in which pkg-config -- cflags -- libs gtk +-2.0 is used to generate the list of header files and library files required for gcc compilation and link programs, the result of running this command on the author's machine is as follows:

    Command: pkg-config -- cflags -- libs gtk +-2.0
    Output:
    -I/usr/include/gtk-2.0-I/usr/lib/gtk-2.0/include-I/usr/include/atk-1.0-I/usr/include/cairo-I/usr/include /pango-1.0-I/usr/include/glib-2.0-I/usr/lib/glib-2.0/include-lgtk-x11-2.0-lgdk-x11-2.0-latk-1.0-lgdk_pixbuf-2.0-lm-lpangocairo-1.0-lfontconfig-lXext -lXrender-lXinerama-lXi-lxrandroid-lXcursor-lXfixes-lpango-1.0-lcairo-lX11-lgobject-2.0-lgmodule-2.0-ldl-lglib-2.0

    2) If you have an unclear definition of a GTK standard API or a type or constant, you can search in the devhelp help program to view the description of a parameter.

    3) The gtk_init (& argc, & argv) function is used to parse and process the passed standard command line parameters. These standard command line parameters are (that is, when running our program, the following parameters passed in through the command line will be automatically processed ):

    • -- Gtk-module
    • -- G-fatal-warnings
    • -- Gtk-debug
    • -- Gtk-no-debug
    • -- Gdk-debug
    • -- Gdk-no-debug
    • -- Display
    • -- Sync
    • -- Name
    • -- Class

    4) gtk_widget_show (window) is used to tell the program that our window component is ready and displayed.
    5) gtk_main (), the program will run here, and then wait for the occurrence of "Event". When an event occurs, the corresponding callback function of this event will be called, after the callback function is executed, it will return to gtk_main () and wait for a new "Event" to occur.
    6) The macro GTK_WINDOW (window) is used for type check and conversion. It forcibly converts a component that can be converted to GtkWindow to the GtkWindow type and passes it to the function.

    Okay. Do you understand hello_dubuntu.c? What! Not understood! It doesn't matter. There will be a lot of such small examples below. If you enter a few more, you will understand it. Believe me!

    Next set notice: a button is added in the window and associated with the "clicked" event for the button, so that some results will be returned after the button is clicked.

    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.