The first GTK + program

Source: Internet
Author: User
Tags gtk

In this section, we'll start writing the first GTK + program.

A super Simple example

We want to "make" a super simple GTK + program. is to display a blank window.

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

This example shows a blank window for us.

Gcc-o simple simple.c ' pkg-config--libs--cflags gtk+-2.0 '

This is the command we use to compile this example. We will explain this simple procedure in detail below.

Gtk_init (&ARGC, &ARGV);

This is where the entire GTK + program is initialized and is an integral part of every GTK + program.

window = Gtk_window_new (gtk_window_toplevel);

Here we first build a widget-- GtkWindow . The type of widget for this widget GTK_WINDOW_TOPLEVEL is: The TopLevel window has a title bar and a border. They agreed to be managed by the window manager.

Gtk_widget_show (window);

After we have generated a widget, it is essential that we use this statement to show the artifacts.

Gtk_main ();

This code statement goes to the main loop. At this point, the GTK + program will silently wait for the event to occur in order to react accordingly.


Figure:simple

Generate a Window

If we do not arrange the location of the window, then the window manager will give us this window "Ann a home." In the next example, we'll walk into the window.

#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), "Center");  Gtk_window_set_default_size (window), Gtk_window;  Gtk_window_set_position (Gtk_window (window), gtk_win_pos_center);  Gtk_widget_show (window);  g_signal_connect_swapped (G_object (window), "Destroy",      G_callback (gtk_main_quit), NULL);  Gtk_main ();  return 0;}

In this example above, I'll delve into the widget (window widget), setting the caption and size for the widget.

Gtk_window_set_title (Gtk_window (window), "Center");

gtk_window_set_title()This function can set a caption for window, and if we don't use this function, GTK + will use the name of the source file as the title of the window.

Gtk_window_set_default_size (Gtk_window (window), 230, 150);

The code snippet above sets the size of the 230x150 pixel for window. It is important to note that the size we refer to here refers to the size of the main window, and does not include the cosmetic or decorative parts provided by the window manager.

Gtk_window_set_position (Gtk_window (window), gtk_win_pos_center);

This Code sets the window to the center of the display.

g_signal_connect_swapped (G_object (window), "Destroy",    G_callback (gtk_main_quit), NULL);

In the previous example, we did not set the window to close when I pressed the "X" in the upper right corner. As we can see, if you run the example program from the command line, the window program will not respond to your just action by default (of course, the latest window manager, such as X11, will be forcibly closed). We have to be clear for this example program to connect the last closed signal (the Destroy signal), which is gtk_main_quit() the function.

The Making of application icons

In the example below, we will make the application icon. Most window managers place icons on the top left and taskbar in the title bar.

#include <gtk/gtk.h>gdkpixbuf *create_pixbuf (const GCHAR * filename) {   gdkpixbuf *pixbuf;   Gerror *error = NULL;   Pixbuf = gdk_pixbuf_new_from_file (filename, &error);   if (!pixbuf) {      fprintf (stderr, "%s\n", error->message);      G_error_free (Error);   }   return pixbuf;} 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), "icon");  Gtk_window_set_default_size (window), Gtk_window;  Gtk_window_set_position (Gtk_window (window), gtk_win_pos_center);  Gtk_window_set_icon (Gtk_window (window), Create_pixbuf ("Web.png"));  Gtk_widget_show (window);  g_signal_connect_swapped (G_object (window), "Destroy",      G_callback (gtk_main_quit), NULL);  Gtk_main ();  return 0;}

The above is for a filled with the window program.

Gtk_window_set_icon (Gtk_window (window), Create_pixbuf ("Web.png"));

gtk_window_set_icon()The function is used to set the icon for the window. The function create_pixbuf() is to generate type data from a PNG image file GdkPixbuf .

Pixbuf = gdk_pixbuf_new_from_file (filename, &error);

According to the official documentation, the function gdk_pixbuf_new_from_file() loads the image data in a file, thus generating a new pixbuf. As for the format of the file containing the image, it is automatically detected by the system. If the function returns a value of NULL, the program will have an error.


Figure:icon

Increase and decrease

Here we will use a simple example to complete the first phase of the "GTK + Programming Tutorial", where we used three artifacts: two buttons and a label. This tag will hold an integer, and two buttons will increment and decrease the number individually.

#include <gtk/gtk.h>gint count = 0;char buf[5];void Increase (gtkwidget *widget, Gpointer label) {count++;  sprintf (buf, "%d", count); Gtk_label_set_text (label, BUF);}  void decrease (Gtkwidget *widget, Gpointer label) {count--;  sprintf (buf, "%d", count); Gtk_label_set_text (label, BUF);}  int main (int argc, char** argv) {gtkwidget *label;  Gtkwidget *window;  Gtkwidget *frame;  Gtkwidget *plus;  Gtkwidget *minus;  Gtk_init (&AMP;ARGC, &AMP;ARGV);  window = Gtk_window_new (gtk_window_toplevel);  Gtk_window_set_position (Gtk_window (window), gtk_win_pos_center);  Gtk_window_set_default_size (Gtk_window (window), 250, 180);  Gtk_window_set_title (Gtk_window (window), "+-");  frame = Gtk_fixed_new ();  Gtk_container_add (Gtk_container (window), frame);  Plus = Gtk_button_new_with_label ("+");  Gtk_widget_set_size_request (plus, 80, 35);  Gtk_fixed_put (Gtk_fixed (frame), plus, 50, 20);  minus = Gtk_button_new_with_label ("-");  Gtk_widget_set_size_request (minus, 80, 35); Gtk_fixed_put (Gtk_fixed (FRAME), minus, 50, 80);  Label = Gtk_label_new ("0");   Gtk_fixed_put (Gtk_fixed (frame), label, 190, 58);  Gtk_widget_show_all (window);  G_signal_connect (window, "Destroy", G_callback (Gtk_main_quit), NULL);  G_signal_connect (Plus, "clicked", G_callback (Increase), label);  G_signal_connect (minus, "clicked", G_callback (Decrease), label);  Gtk_main (); return 0;}

The function of this example code is to increase and decrease the value of the object Gtklabel.

G_signal_connect (Plus, "clicked",     g_callback (Increase), label);

We link the callback function increase() with the Add button. It is also worth noting that we use the label as a parameter to call the callback function. This allows the label to be processed in the callback function increase ().

count++;sprintf (buf, "%d", count); gtk_label_set_text (label, BUF);

In the "increment" callback function, increase the number. The number in the label will then increase accordingly.


The first GTK + 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.