GTK+-3.0学习笔记(一)——Basics

GTK±3.0学习笔记(一)——Basics

参考该官方文档

这篇文章的目的:创建一个简单的窗口,并籍此简单介绍一个GTK程序是如何运行的。

首先,放上代码:

#include <gtk/gtk.h>

static void activate(GtkApplication *app, gpointer user_data);

/**
 * To create a GtkApplication object and run it
 */
int main(int argc, char *argv[])
{
    
    
	GtkApplication *app;  // gtk application
	int status;

	// "org.gtk.example": a name of app
	// FLAGS: as an input for app
	app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);  // initialize
	
	// connect activate signal to the func activate
	// this signal will be sent when app is launched with g_application_run
	g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
	// start to run the prog
	status = g_application_run(G_APPLICATION(app), argc, argv);
	// free the app object
	g_object_unref(app);

	return status;
}

/**
 * Construct window
 * @param app       gtk application
 * @param user_data ???
 */
static void activate(GtkApplication *app, gpointer user_data)
{
    
    
	GtkWidget *window;

	// create a window and store it
	window = gtk_application_window_new(app);
	// set title
	// GTK_WINDOW() will check if the pointer is an instance of the GtkWindow class
	gtk_window_set_title(GTK_WINDOW(window), "Hello, world!");
	gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
	// show window
	gtk_widget_show_all(window);
}

接下来,对代码进行解释

从函数入——main函数进入:

main函数主要的工作是:创建一个GtkApplicatio并且运行
GtkApplication相当于程序的主体。

创建GtkApplication使用gtk_application_new函数
该函数传入两个参数:identityGFLAG
即app的名字,GFLAGS以后有用到再说

其次,将app中的一个信号量activate与函数activate链接起来。
当程序运行的时候,会触发一个信号activate,由此,函数activate被运行。
使用g_signal_connect函数实现该功能

在初学者阶段,我不认为我需要弄明白每个函数的用途。

使用g_application_run运行一个GTK程序。
该函数会返回一个值,说明程序的运行结果

最后使用g_object_unref来销毁GtkApplication

这是该函数的主体的流程:
创建app->链接信号量->运行程序->销毁app

接下来看看activate函数做了哪些工作:
这个函数定义了窗口的主体
首先,使用gtk_application_window_new创建了一个新的窗口
该窗口是基于GtkApplication创建的。
然后,对窗口的属性进行设置
gtk_window_set_title中的GTK_WINDOW()是一个宏,它的作用是将指针转化为GTK_WINDOW*指针,同时对转化的对象进行检验,查看其是否符合要求。我们可以直接使用(GTK_WINDOW*)window进行转换,但是不安全。
同理,main函数中的G_APPLICATION宏也是这个意思

最后,使用gtk_widget_show_all函数展示窗口。

窗口关闭后,函数g_application_run会返回一个状态,表明函数的运行状态。

这就是一个基本的、什么都不做的GTK函数的运行流程。

附makefile:

helloworld: helloworld.o
	gcc -o helloworld helloworld.o \
	-pthread \
	-lgtk-3 -lgdk-3 -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -latk-1.0 \
	-lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0


helloworld.o: helloworld.c
	gcc -c helloworld.c \
    -I/usr/include/gtk-3.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/at-spi-2.0 \
	-I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include \
	-I/usr/include/gtk-3.0 -I/usr/include/gio-unix-2.0 -I/usr/include/cairo \
	-I/usr/include/pango-1.0 -I/usr/include/fribidi -I/usr/include/harfbuzz \
	-I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 \
    -I/usr/include/uuid -I/usr/include/freetype2 -I/usr/include/libpng16 \
    -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libmount -I/usr/include/blkid \
    -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include 

clean:
	rm helloworld helloworld.o

remake:
	make clean
	make

猜你喜欢

转载自blog.csdn.net/weixin_45206746/article/details/112005999