基础

我们用一个最简单的程序来开始对GTK的介绍,下面的程序将创造一个200×200像素的窗体。
window_default

新建一个名为 example-0.c 的文件,写入如下内容:

  1. #include <gtk/gtk.h>
  2. int main (int argc, char *argv[])
  3. {
  4. GtkWidget *window;
  5. gtk_init (&argc, &argv);
  6. window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  7. gtk_window_set_title (GTK_WINDOW (window), "Window");
  8. g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  9. gtk_widget_show (window);
  10. gtk_main ();
  11. return 0;
  12. }

然后在终端输入以下命令用GCC编译程序:

  1. gcc `pkg-config --cflags gtk+-3.0` -o example-0 example-0.c `pkg-config --libs gtk+-3.0`

注:要查找更多编译GTK程序的信息,请查看手册中编译GTK+应用的部分。

所有的GTK+程序必须包括gtk/gtk.h,这个头文件声明了GTK+程序需要的函数、类和宏。

注:即使GTK+安装了多种头文件,只有顶层的gtk/gtk.h 能被第三方代码直接引入。如果引入任意一个其他的头文件,编译器都会报错。

我们接下来进入main函数,将会声明一个GtkWidget类型的指针变量window
下面一行将会调用gtk_init()函数,这个函数是GTK+程序的初始化函数,它将设置GTK+、类系统和与窗口环境的连接。

注:要查找更多GTK+程序的命令参数,请查看手册中运行GTK+程序的部分。

调用gtk_window_new()函数将会创造一个新的GtkWindow并将其储存在window变量中。并且,这个窗体的类型是GTK_WINDOW_TOPLEVEL,这也就意味着这个GtkWindow将会被当前的系统管理:这个窗体将会根据不同的系统平台产生一个框架、一个标题栏和窗口控件。

GtkWindow被破坏时,我们将“destroy”信号连接到gtk_main_quit()函数以终止这个程序。这个函数将会在之后终止由gtk_main()函数启动的GTK+程序的主循环。“destroy”信号会在一个窗口部件被破坏时触发,也会是在调用gtk_widget_destroy()或者在这个窗口部件失去母体控件时触发。最顶端的GtkWindows会在关闭按钮被点击时被破坏。

GtkWidgets默认是隐藏的,通过在一个控件上调用gtk_widget_show(),我们将能设置其为可见。所有这些工作都将在主循环开始后被完成。

最后一行调用了gtk_main()。这个函数就会启动GTK+程序的主循环并且在gtk_main_quit()函数被调用之前都阻止main()的控制流。

当程序运行时,GTK+一直接收事件。有一些输入事件是由用户与程序互动时产生的,但也有一些事件,比如来自窗口管理器或者其他程序的信息。GTK+处理这些事件和信息,然后触发信号。为这些信号连接handles就是让你的程序为用户输入做出正确响应的方法。

下面这个例子有点复杂,它将展示GTK+的能力。按照程序设计语言和库的古老传统,这个程序也叫Hello,World

hello_worldpng

Example 1. Hello World in GTK+

新建一个名为 example-1.c 的文件,写入如下内容:

  1. #include <gtk/gtk.h>
  2. /* This is a callback function. The data arguments are ignored
  3. * in this example. More on callbacks below.
  4. */
  5. static void print_hello (GtkWidget *widget,
  6. gpointer data)
  7. {
  8. g_print ("Hello World\n");
  9. }
  10. static gboolean on_delete_event (GtkWidget *widget,
  11. GdkEvent *event,
  12. gpointer data)
  13. {
  14. /* If you return FALSE in the "delete_event" signal handler,
  15. * GTK will emit the "destroy" signal. Returning TRUE means
  16. * you don't want the window to be destroyed.
  17. *
  18. * This is useful for popping up 'are you sure you want to quit?'
  19. * type dialogs.
  20. */
  21. g_print ("delete event occurred\n");
  22. return TRUE;
  23. }
  24. int main (int argc, char *argv[])
  25. {
  26. /* GtkWidget is the storage type for widgets */
  27. GtkWidget *window;
  28. GtkWidget *button;
  29. /* This is called in all GTK applications. Arguments are parsed
  30. * from the command line and are returned to the application.
  31. */
  32. gtk_init (&argc, &argv);
  33. /* create a new window, and set its title */
  34. window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  35. gtk_window_set_title (GTK_WINDOW (window), "Hello");
  36. /* When the window emits the "delete-event" signal (which is emitted
  37. * by GTK+ in response to an event coming from the window manager,
  38. * usually as a result of clicking the "close" window control), we
  39. * ask it to call the on_delete_event() function as defined above.
  40. *
  41. * The data passed to the callback function is NULL and is ignored
  42. * in the callback function.
  43. */
  44. g_signal_connect (window, "delete-event", G_CALLBACK (on_delete_event), NULL);
  45. /* Here we connect the "destroy" event to the gtk_main_quit() function.
  46. * This signal is emitted when we call gtk_widget_destroy() on the window,
  47. * or if we return FALSE in the "delete_event" callback.
  48. */
  49. g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  50. /* Sets the border width of the window. */
  51. gtk_container_set_border_width (GTK_CONTAINER (window), 10);
  52. /* Creates a new button with the label "Hello World". */
  53. button = gtk_button_new_with_label ("Hello World");
  54. /* When the button receives the "clicked" signal, it will call the
  55. * function print_hello() passing it NULL as its argument.
  56. * The print_hello() function is defined above.
  57. */
  58. g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
  59. /* The g_signal_connect_swapped() function will connect the "clicked" signal
  60. * of the button to the gtk_widget_destroy() function; instead of calling it
  61. * using the button as its argument, it will swap it with the user data
  62. * argument. This will cause the window to be destroyed by calling
  63. * gtk_widget_destroy() on the window.
  64. */
  65. g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
  66. /* This packs the button into the window. A GtkWindow inherits from GtkBin,
  67. * which is a special container that can only have one child
  68. */
  69. gtk_container_add (GTK_CONTAINER (window), button);
  70. /* The final step is to display this newly created widget... */
  71. gtk_widget_show (button);
  72. /* ... and the window */
  73. gtk_widget_show (window);
  74. /* All GTK applications must have a gtk_main(). Control ends here
  75. * and waits for an event to occur (like a key press or a mouse event),
  76. * until gtk_main_quit() is called.
  77. */
  78. gtk_main ();
  79. return 0;
  80. }

然后在终端输入以下命令用GCC编译程序:

  1. gcc `pkg-config --cflags gtk+-3.0` -o example-1 example-1.c `pkg-config --libs gtk+-3.0`