GTK+ 2.x初学者指南.第一章
来源:互联网 发布:libreoffice python 编辑:程序博客网 时间:2024/06/10 16:09
GTK+ 2.x Tutorial for Beginners
GTK+ 2.x初学者指南
作者:Jan Bodnar
原文出自:http://zetcode.com/tutorials/gtktutorial/firstprograms/
This tutorial is for beginners describing some of the most common widgets written by Jan Bodnar.
由Jan Bodnar编写的这本初学者指南描述了一些常用的构件。
First programs in GTK+
第一个GTK+程序
In this part of the GTK+ programming tutorial, we will create our first programs in GTK+
在《GTK初学者指南》的本章中,我将使用GTK+开发我们的第一个程序。
Simple example
简单例子
We start with a very simple example. We will show a basic 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 will show a basic window on screen.
这个例子将在屏幕上显示出一个基本窗体。
gcc -o simple simple.c `pkg-config --libs --cflags gtk+-2.0`
This is how we compile the example.
使用上面这个命令行来编译前面的例子。
gtk_init(&argc, &argv);
Here we initiate the GTK+ library.
初始化GTK+库。
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
We create a GtkWindow widget. The window type is GTK_WINDOW_TOPLEVEL. Toplevel windows have a titlebar and a border. They are managed by the window manager.
我们创建一个GtkWindow构件,窗体的类型是GTK_WINDOW_TOPLEVEL。顶层窗体中附带一个标题栏和一个边框,由窗口管理器负责管理
gtk_widget_show(window);
After we have created a widget, we must show it.
当创建完一个构件后,应将它显示出来。
gtk_main();
This code enters the GTK+ main loop. From this point, the application sits and waits for events to happen.
这个代码的作用是使程序进入GTK+主循环。由此看出,程序将停止并等待即将发生的事件。
Figure: Simple
Centering the window
置中窗体
If we do not position the window ourselves, the window manager will position it for us. In the next example, we will center 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(GTK_WINDOW(window), 230, 150);
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 our example, we center the window, set a title and size for the window.
在这个例子中,我们置中了一个窗体,并为其设置标题和窗体大小。
gtk_window_set_title(GTK_WINDOW(window), "Center");
The gtk_window_set_title() function will set a window title. If we do not set a title ourselves, the GTK+ will use a name of a source file as a title.
gtk_window_set_title() 函数用于给窗体设置标题。如果没给窗体设置标题,GTK+会使用源程序名作为窗体标题。
gtk_window_set_default_size(GTK_WINDOW(window), 230, 150);
This code sets the size of the window to 230x150 pixels. Note, that we are talking about the client area, excluding the decorations provided by the window manager.
这句代码的作用是将窗体的大小设置为230x150像素,也就是我们说的窗体客户区,包括窗口管理器提供的装饰构件。
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
This code centers the window.
这句代码是将创建的窗体置于屏幕的中间位置。
g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
In the previous example, the window was not completely destroyed, when we clicked on the x button. We can see it, if we lauch the example from the command line. The window does not react to the destroy signal by default. We must explicitely terminate the application by connecting the destroy signal to the gtk_main_quit() function.
在当前例子中,当我们单击关闭按钮时,窗体不能被完全销毁。如果我们从命令行启动这个程序,就能看见窗体。但窗体并不响应默认的销毁信号,我们必须明确地使用gtk_main_quit()函数接管destroy信号来终止应用程序。
The application icon
应用程序图标
In the next example, we show the application icon. Most window managers display the icon in the left corner of the titlebar and also on the taskbar.
在下面这个例子中,我们将显示应用程序图标。大多数窗口管理器在标题栏的左角处显示图标,也可在任务栏上显示。
#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");