GTK Getting Started Tutorial

GTK Getting Started Tutorial

1. Introduction to GTK

  GTK (GIMP Toolkit) is a cross-platform graphics toolkit whose source code is distributed under the LGPL license. Originally written for GIMP, it has become a general-purpose graphics library with powerful functions and flexible design. It is one of the mainstream development tools for developing graphical interface applications under GNU/Linux. Of course, GTK also supports cross-platform, supporting Unix-like systems, Windows, and even mobile phone platforms.
  GTK is written in C language, so its native APIs are all C-oriented. At the same time, a major feature of GTK is that it implements object-oriented features at the C language level. GTK is completely free and based on the LGPL agreement, which ensures that private software can use GTK through links without opening the software source code, which is more friendly to commercial applications, which is different from the GPL agreement. It is also the LGPL agreement that made Gnome (written based on GTK) outperform KDE (written based on QT) in the early years.
  According to the current development trend, GTK+ is becoming more and more the native toolkit of Linux and GNOME, and there is less and less consideration for cross-platform. GNOME is still the most mainstream desktop system on the Linux platform, and desktops such as Pantheon and Cinnamon are just GNOME shells, and the bottom layer is also the GNOME technology stack, so GTK+ will still make great progress in this regard.
insert image description here

2. GTK and GTK+

  The above only mentioned gtk, but what I want to learn is gtk+. What is the relationship between them? Perter Mattis, one of the authors of gtk+, said that the gtk he originally developed included three sets of function libraries, libglib, libgdk, and libgtk. These libraries did not adopt an object-oriented mechanism, so they could not realize the reuse of components, and the message mechanism used standard callbacks mechanism, rather than the signal mechanism provided by the current gtk+. "+" is used to distinguish the original version from the new version. gtk+ is still completely developed using C language, but it flexibly uses the idea of ​​object-oriented design (OOD) in the design (who said that only languages ​​​​such as C++, java, and C# can be OOD).

3. The first GTK program under Linux

3.1 Check if GTK is installed in the system

  The pkg-config command can view the detailed information of the installed library.

$ pkg-config  --list-all | grep gtk
gtk-sharp-3.0                  Gtk - Gtk
gtk+-unix-print-2.0            GTK+ - GTK+ Unix print support
gtk+-x11-2.0                   GTK+ - GTK+ Graphical UI Library (x11 target)
gtk+-2.0                       GTK+ - GTK+ Graphical UI Library (x11 target)

3.2 The first GTK program example

#include <gtk/gtk.h>
int main(int argc,char *argv[])
{
    
    
	/* GtkWidget 是构件的存储类型 */
	GtkWidget *window;
	/*每一个GTK程序都要调用*/
	gtk_init(&argc, &argv);
	/*创建一个新的窗口*/
	window= gtk_window_new(GTK_WINDOW_TOPLEVEL);
	/*设置窗口标题*/
	gtk_window_set_title(GTK_WINDOW(window),"GTK Hello");
	/*设置窗口大小*/
	gtk_widget_set_usize(window, 200, 200);
	/*显示窗口*/
	gtk_widget_show_all (window); 
	/*所有的 GTK 程序必须有一个 gtk_main() 函数。
	程序运行停在这里等待事件 (如键盘事件或鼠标事件) 的发生。 */
	gtk_main();
	return 0;
}
  • Program compiles:
gcc main.c  `pkg-config --cflags --libs gtk+-2.0`

insert image description here

  • Compile parameters:

  pkg-config --cflags gtk±2.0 lists the include directory, pkg-config --libs gtk±2.0 lists the compiled and linked libraries, and can also be combined, like this: pkg-config --cflags --libs gtk±2.0 .
  Here "command substitution" is used. Command substitution (command substitution) makes it possible to capture the output of one command and replace it in another command. This single quote is not the one to the left of the Enter key, but the one below the ESC key.

  • Commonly used libraries when connecting:
• GTK 库(-lgtk),构件库,基于GDK。
• GDK 库(-lgdk),Xlib库的封装(wrapper)。
• gdk-pixbuf 库(-lgdk_pixbuf),图像处理库。
• Pango 库(-lpango),处理国际化文本。
• gobject 库(-lgobject),包含作为 GTK 基础的类型系统。
• gmodule 库(-lgmodule),动态运行库。
• GLib 库(-lglib),包含各种函数; GTK是基于 GLib 的,因此你总需要这个库。
• Xlib 库(-lX11),GDK 要使用。
• Xext 库(-lXext),包含共享内存位图和其它 X 扩展。
• math 库(-lm),数学库,这个被 GTK 因各种目的而使用。

3.3 Introduction of related function interface

  1. gtk initialization gtk_init

void gtk_init (int *argc, char ***argv)
function: gtk initialization, which will be called in each gtk program. This function sets the default video (visual) and color mapping mode (color map).

  1. gkt main processing loop gtk_main

void gtk_main (void)
function: 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 to occur. But before calling the gtk_main_quit function, the call to the gtk_main function does not return, even if you close the window, the program is still running (the above example demonstrates this effect), in this case, we should first establish before calling the gtk_main function Callback function, and set GTK+ so that when certain signals appear, the application software will be executed instead, and the application software will then process it.

  • References:
      1. GTK2.0+ Getting Started Tutorial
      2. GTK official website: https://www.gtk.org/

Guess you like

Origin blog.csdn.net/weixin_44453694/article/details/127341241