Start from here @ GTK + 2.0 Chinese tutorial serialization

Source: Internet
Author: User
Tags function examples gtk
Start from here

First, download the GTK source program and install it. You can always
Ftp.gtk.org
Get the latest version. You can also in http://www.gtk.org/
To view information about other GTK source programs. GTK is configured using GNU Autoconf. After decompression, enter./Configure -- Help
View the Option List

The GTK source code release package contains the code of all examples in the tutorial. Each example contains makefiles for convenient compilation.

We will start with a simple program as much as possible. This program creates a 200x200 window and cannot exit unless you kill it from the shell.

#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;
}

You can use GCC to compile the above program:

GCC base. C-o base 'pkg-config -- cflags -- libs GTK +-2.0'

Compilation of the hello World Program below for infrequently used compilation Parameters
.

All programs should containGTK/GTK. h
Which declares variables, functions, and data structures. These things will be used in your program.

Next line:

gtk_init (&argc, &argv);

This
Function gtk_init (Gint * argc, gchar *** argv)
In the application. This function sets the default video (visual) and color ing mode (color map), and then calls the function gdk_init (Gint
* Argc, gchar *** argv ). This function initializes the library to be used, sets the default signal processing, and checks the command line parameters passed to your program to find one of the following:

  • -- GTK-Module
  • -- G-fatal-Warnings
  • -- GTK-Debug
  • -- GTK-no-Debug
  • -- Gdk-Debug
  • -- Gdk-no-Debug
  • -- Display
  • -- Sync
  • -- Name
  • -- Class

These parameters will be deleted from the parameter table and left unidentifiable or ignored for your program. This creates a set of standard parameters that can be accepted by all GTK programs.

The following two lines of programs are created and a window is displayed.

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_show (window);

Gtk_window_toplevel
Parameters are modified and placed in the window manager. A 0x0 window is not created here. The default size of a window without sub-components is 200x200, so that you can still operate on it.

The gtk_widget_show () function lets GTK know that we have set the attributes of the component to display it.

The last line enters the main processing cycle of GTK.

  gtk_main ();

Gtk_main () is another function call that can be seen in each GTK program. When the program runs here, GTK will "fall asleep" and wait for X events (such as button or keyboard Press), timeouts, or file IO notifications. In our example, the event is ignored.

Write Hello world with GTK

Well, now I want to write a program with only one button component. This is a standard GTK Hello world.


# Include <GTK/GTK. h>

/* This is a callback function. The data parameter is ignored in this example.
* More callback function examples are provided. */
Void Hello (gtkwidget * widget,
Gpointer data)
{
G_print ("Hello World/N ");
}

Gint delete_event (gtkwidget * widget,
Gdkevent * event,
Gpointer data)
{
/* If your "delete_event" signal processing function returns false, GTK sends a "Destroy" signal.
* Returns true. You do not want to close the window.
* If you want to pop up "are you sure you want to exit ?" Dialog box. */

G_print ("delete event occurred/N ");

/* If it is set to true, the program will be closed. */

Return true;
}

/* Another callback function */
Void destroy (gtkwidget * widget,
Gpointer data)
{
Gtk_main_quit ();
}

Int main (INT argc,
Char * argv [])
{
/* Gtkwidget is the component storage type */
Gtkwidget * window;
Gtkwidget * button;

/* This function must be called in all GTK programs. Parameters are parsed from the command line and sent to the Program */
Gtk_init (& argc, & argv );

/* Create a new window */
Window = gtk_window_new (gtk_window_toplevel );

/* When the window receives the "delete_event" signal (this signal is sent by the window manager, usually "closed"
* Option or the close button on the title bar). Let's call the previously defined delete_event () function.
* If the value of the data parameter passed to the callback function is null, it will be ignored by the callback function. */
G_signal_connect (g_object (window), "delete_event ",
G_callback (delete_event), null );

/* Here we connect the "Destroy" event to a signal processing function.
* Call the gtk_widget_destroy () function for this window or return false values in the "delete_event" callback function.
* This event is triggered. */
G_signal_connect (g_object (window), "Destroy ",
G_callback (destroy), null );

/* Set the Border width. */
Gtk_container_set_border_width (gtk_container (window), 10 );

/* Create a New button labeled "Hello World. */
Button = gtk_button_new_with_label ("Hello World ");

/* When the button receives the "clicked" signal, it calls the hello () function and transmits null
* It is used as a parameter. The Hello () function is defined previously. */
G_signal_connect (g_object (button), "clicked ",
G_callback (Hello), null );

/* When you click the button, the window is closed by calling gtk_widget_destroy (window.
* The "Destroy" signal is sent from here or from the window manager. */
G_signal_connect_swapped (g_object (button), "clicked ",
G_callback (gtk_widget_destroy ),
Window );

/* Put the button in a window (a GTK container. */
Gtk_container_add (gtk_container (window), button );

/* The last step is to display the newly created button and window */
Gtk_widget_show (button );
Gtk_widget_show (window );

/* All GTK programs must have a gtk_main () function. Program running stops here
* Wait for the occurrence of an event (such as a keyboard event or a mouse event. */
Gtk_main ();

Return 0;
}
<Previous Home Next >>>
Introduction   Compile the hello World Program

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.