1. about naming coventions
a. All gtk functions or classes(structures) are started with "gtk" or "Gtk" as prefixes. For instance,
gtk_widget_destroy, GtkLabel, GtkWidget. All glib functions or classese are prefixed with g, for example
g_signal_connect, g_ascii_strcasecmp, G_OBJECT, g_object_set, g_object_get
b. The operations for each classes was prefixed by class name. for example, gtk_window_new,
gtk_button_new_with_label. GtkWindow, GtkLabel.
c. All returning types and arguments are pointers to classes. For example, GtkWidget *gtk_window_new(),
gtk_window_set_title( GtkWindow *window, gchar *str );
d. All constructor of each classes return the pointer to GtkWidget.
e. You can covert types using macros. GtkWidget *window; GtkWindow *window = GTK_WINDOW( window );
f. About signal connecting function, it takes pointer to G_OBJECT only, the final argument gpointer is
the data you wanna pass into event handling function.
g. Some event handling function should look like this:
gboolean event_happened( GtkWidget *widget, GdkEvent *event, gpointer data );
the first arg widget is the widget which received the signal, event is the event and
pointer is the data you want to pass.
Of course, you can use any other form of event handler, like:
void event_handle( GtkWidget *widget, gpointer data );
This event handler is much more popular than others
void event_emitted( GtkWidget *widget1, GtkWidget *widget2 );
the arguments are pointers. Since you can cast a pointer to any other
type, so you can replace GtkWidget with any other kind widget you like. Generally, First arg: widget1 is
the one from which signals are emitted. It is also the widget to which you connect signal. The second
argument of event handler: widget2, also can be casted to other pointers you like. It is passed from
final argument of g_signal_connect.
Sometimes, your handlers must operate more than one widgets. To solve the problem, you can use a structure
which holds all the necessary widgets. You can pass a pointer to such structure into your handler. E.G.
You can connect signal and handler this way:
The handler should look like this:
void button_clicked( GtkButton *button, Data *data ); h.For some operations there are more than one function. We get overloading here. But C does not
support overloading explictly. We solve the problem by adding suffixes to functions indicating
overloading. e.g.:
gtk_button_new(), gtk_button_new_with_label( const char *label ),
gtk_button_new_with_mnemonic( const char *mnemonic ). They have the same purpose ---- to create
a new button. Such overloading is quite popular in GTK+.
i. Once you have add some widgets to some containers, you cannot add it to other containers any more.
That's why we have to new a label after adding it to a vbox. Adding a widget to a container is make
the container parent of widget. When releasing the widgets, the child will be released if the parent
is being freed. Problem occurs when releasing widgets if you add a widgets to more than one container.
It could be released twice which is a fatal error. As a result, you can never add one widget to more
than one containers.
j. There are some internal event handler functions such as destroy -- gtk_widget_destroy. So you can
connect them directly to destroy(gtk_main_quit), gtk_widget_destroy. It is the same to write it by
yourself.
k. Many times, some events have happened, though you have not handled that. A font button, for instance,
would keep its last font after being clicked. No matter whether you handle the signal 'font_set', it
would be there until next click. In fact, in your handler, you do nothing but retrieve the current
also last font from the button and set the font to some widgets. Therefore, it is the same business
to retrieve the font when we are about to use it.
2. Tricks about widgets
1. GtkFileChooser, GtkFileChooserDialog, GtkFileChooserButton
when you use those widgets, you should handle signal "file_set" rather than others. "file_set" is the
signal emitted when user do a change of file chooser, i.e. select a file, select another file.
Signal "selection_changed" will be emitted either when user do a change or when a function change
the current path. We should use "selection_changed" to monitor file chooser's change. While "file_set"
is for detecting a file has been selected.
2. GtkWindow
If you want to add asking dialog before closing, you should catch signal "delete_event". And the handler's
prototype should look like this: gboolean before_exit( GtkWidget *, GdkEvent *, gpointer );
returning FALSE if you don't quit, else return TRUE to quit.
3. GtkHBox, GtkVBox
To keep your widgets look homogenous, you have to pack all of them onto boxes. If you pack a hbox and
a check button onto a vbox, the result is unbalanced. So you'd better arrange all your widgets onto
boxes.
4. GtkLabel
In order to arrange some widgets onto right place, you can use some labels with empty text as a
placeholder. You want to arrange a button called "Exit", for instance, on the right-down cornor
of the window, The easiest way to achieve that is place a label unpon button and a label left to
the button.
5. GtkFontButton, GtkColorButton, GtkFileChooserButton, and alike.
They are buttons, right. But they are special buttons. To realize their true mission, you cannot
treat them as buttons. So you should handle the signal 'font_set', 'color_set' or 'file_set' instead
of 'clicked'.
When you are using GtkFileChooserButton, you'd better use event 'selection_change', although it will be
emitted when setting its properties. The event 'selection_changed' will be emitted when you change a
selection -- that is when you click the button, select another directory or file. Event 'file_set',
however, will only be emitted when you click "Others", opening chooser dialog, selecting a file and clicking
"OK" in dialog. So, if you connect event 'file_set' for it, when you click the folders in the button's list,
nothing would change. Example: explorerlist.c
6. GtkToggleButton, GtkRadioButton, GtkCheckButton
You don't have to write handlers for signal 'toggled'. You can test whether it is active (selected)
when using the option. If you write a handler, you have to test whether it is active, too, in order to
set some flag indicating an option.
7. GtkDialog.
Dialog is like a window which can hold many things. You can add basic widgets like labels, buttons.
In addition, you can add GtkAssistant, progress bar, window on it, too. We are happy to say the dialog's
constructor again:
You can use labels from stock like
GTK_STOCK_YES, GTK_STOCK_NO, GTK_STOCK_CANCEL, GTK_STOCK_APPLY, etc.
You can also use some responses predefined by GTK+ like:
GTK_RESPONSE_YES, GTK_RESPONSE_NO,
GTK_RESPONSE_CANCEL, GTK_RESPONSE_APPLY, etc. There is a container vbox in dialog structure, so
you can add anything onto it.
We use the responses of dialog to handle different clicks on buttons, say, returning GTK_RESPONSE_YES means
button 'Yes' has been clicked, returning GTK_RESPONSE_CANCEL means button 'cancel' has been clicked. A
typical one is like this:
In general, we would destroy the dialog after clicking one of its buttons by adding:
gtk_widget_destory( dialog );
after handling return values.
At some other times, we want to keep the dialog, because we are not done yet. Some properties panel dialog,
for example, have three buttons, Cancel, OK and Apply. Clicking Cancel will quit the dialog and do nothing.
Click OK will apply current settings and quit the dialog. Clicking Apply, however, will apply the settings,
but don't quit the dialog. For those dialogs, we should put gtk_widget_destroy after GTK_RESPONSE_CANCEL and
GTK_RESPONSE_OK. But for Apply we should use some other tricks --- we have to keep the dialog and get return
values, so a while loop is necessary. We usually do like this:
We will never leave the dialog unitl 'Cancel' or 'OK' is clicked.
8. The event handler for signal 'delete-event' of window must be like this:
static gboolean window_close( GtkWindow *window, GdkEvent *event, gpointer data );
分享到:
相关推荐
GTK+ 参考手册 for GTK+ 2.14.3,最新英文版 GTK+ is a library for creating graphical user interfaces. It works on many UNIX-like platforms, Windows, and on framebuffer devices. GTK+ is released under ...
Foundations of GTK+ Development (PDF 英文版) By Andrew Krause ISBN10: 1-59059-793-1 ISBN13: 978-1-59059-793-4 630 pages Apr 2007 (Apress) GTK+ is one of the most influential graphical toolkits for...
基于C gtk/gtk+ sqlite3选课系统的开发
gtk+开发工具gtk+开发工具gtk+开发工具
最新的基于GTK+的单词数值计算器基于linux开发,中文显示,见面优化,配带有相关文件,流程图,代码
GTK+(GIMP Toolkit)是一套跨多种平台的图形工具包,按LGPL许可协议发布的。虽然最初是为GIMP写的,但目前已发展为一个功能强大、设计灵活的一个通用图形库。特别是被GNOME选中使得GTK+广为流传,成为Linux下开发图形...
GTK+程序设计中文版 .pdf
gtk+-bundle_2.18.7-20100213_win32 gtk+-bundle_2.18.7-20100213_win32
gtk+-2.24.0.tar.bz2 GTK+源代码 自己需要的
压缩包包含gtk+-bundle_2.24.10-20120208_win32文件和gtk+ Visual Studio 2008环境搭建文档说明
本书详细介绍了两种Linux图形用户界面开发技术:GTK+/GNOME。主要内容包括:GUI的基本概念,GLib,GTK+及GNOME的简介,各种GUI元素的创建与使用,bIDE,Glade等等。最后通过两个综合实例来巩固所学知识。本书实例...
注意: GTK+ 3.x版本跟Python2.7不兼容,如果在Python下使用GTK,需要安装2.x版的gtk+ (在windows上如此)
GTK+2.0编程范例-书籍和书中代码.tar.gz GTK+2.0编程范例 书本比较清晰 加上有代码 GTK入门和提高必备
GTK+虽然是Linux下的开发工具包,但是因为其跨平台特性,我们可能需要在Windows 环境下进行程序开发。所以如何在Windows下配置GTK+的开发环境,是许多Gtk学习者的实际需求。 本文在下文基础上增加了GTK+3.0模板修改...
英文名称:The GTK+ tutorial 本GTK+程序初级教程采用了“引导”式的方法来教你如何使用GTK+。本教程所用的编程语言为C语言。并且整个教程中方法已经在Linux中测试通过。本教程适用GTK+的初级兼代中级水平的程序员...
gtk+-2.6.6.tar.bz2
第五部分包括三个附录,附录A是书中使用的示例GnomeHello的源代码,附录B介绍了一些与Gtk+/Gnome编程相关的在线资源,附录C是Gtk+/Gnome对象的简要介绍。本书中的Gtk+构件示例都来自于GTK 1.2.3软件包的示例。如果...
关于图形库GTK+2.0的一本教材,例子丰富……对刚刚入门的人应该有用