📄 gtk_tut-3.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Draft//EN"><HTML><HEAD><meta http-equiv="pragma" content="no-cache"><TITLE>GTK导引: 下一步</TITLE></HEAD><BODY><A HREF="gtk_tut-2.html" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut-2.html"><IMG SRC="prev.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/prev.gif" ALT="Previous"></A><A HREF="gtk_tut-4.html" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut-4.html"><IMG SRC="next.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/next.gif" ALT="Next"></A><A HREF="gtk_tut.html#toc3" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut.html#toc3"><IMG SRC="toc.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/toc.gif" ALT="Contents"></A><HR><H2><A NAME="s3">3. 下一步</A></H2><P><H2><A NAME="ss3.1">3.1 资料型态</A></H2><P>有些东西您可能在前面的范例中已经看到, 这需要多解释一下. 像gint, gchar等等. 这些是为了取得绝对乾净的独立性, 如资料大小等等.像"gint32"就是个很好的范例, 其目的是维持到任意平台均为32bits,不管是64 bit alpha或是32 bit i386. 其定义是极其直接而且直觉的.它们都被定义在glib/glib.h (被gtk.h所include).<P>您也看到像在GtkWidget这一类的东西. GTK是物件导向的设计, 而widget则是其中的物件.<P><H2><A NAME="ss3.2">3.2 更多关於信号处理函数</A></H2><P>我们来看看gtk_signal_connect宣告.<P><BLOCKQUOTE><CODE><PRE>gint gtk_signal_connect (GtkObject *object, gchar *name, GtkSignalFunc func, gpointer func_data);</PRE></CODE></BLOCKQUOTE><P>看到gint的返回值? 这是个标明您的callback函数的标签值.像之前所说的, 每个信号及物件可以有好几个callback, 每个会以它们所接上的顺序被轮流执行到.您可以用以下这个函数来移除这个callback函数:<BLOCKQUOTE><CODE><PRE>void gtk_signal_disconnect (GtkObject *object, gint id);</PRE></CODE></BLOCKQUOTE>你可以透过您想要移除的widget handler,给定id, 来解除信号处理函数.<P>您也可以用:<P><BLOCKQUOTE><CODE><PRE>gtk_signal_disconnect_by_data (GtkObject *object, gpointer data);</PRE></CODE></BLOCKQUOTE><P>这玩意我倒没用过, 我真得不晓得要怎麽用 :)<P>另一个函数可以解除所有的处理函数:<BLOCKQUOTE><CODE><PRE>gtk_signal_handlers_destroy (GtkObject *object);</PRE></CODE></BLOCKQUOTE><P>这个函数到底是自己解释了自己的功能. 它移除了该物件所有的信号处理函数.<P><H2><A NAME="ss3.3">3.3 Hello World的加强版</A></H2><P>我们来看看一个稍经改良的hello world, 这是个callback的不错的例子.这也会介绍到我们下一个主题, 封装物件.<P><BLOCKQUOTE><CODE><PRE>#include <gtk/gtk.h>/* 新改进的callback. 输入到该函数的资料被输出到. */void callback (GtkWidget *widget, gpointer *data){ g_print ("Hello again - %s was pressed\n", (char *) data);}/* another callback */void destroy (GtkWidget *widget, gpointer *data){ gtk_main_quit ();}int main (int argc, char *argv[]){ /* GtkWidget is the storage type for widgets */ GtkWidget *window; GtkWidget *button; GtkWidget *box1; /* this is called in all GTK applications. arguments are parsed from * the command line and are returned to the application. */ gtk_init (&argc, &argv); /* create a new window */ window = gtk_window_new (GTK_WINDOW_TOPLEVEL); /* 这是个新函数, 它设定title到新视窗上"Hello Buttons!" */ gtk_window_set_title (GTK_WINDOW (window), "Hello Buttons!"); /* 用这样会比较简单一点. */ gtk_signal_connect (GTK_OBJECT (window), "destroy", GTK_SIGNAL_FUNC (destroy), NULL); /* 设定边框宽度. */ gtk_container_border_width (GTK_CONTAINER (window), 10); /* 我们产生一个box来封装物件. 这一点会在"packing"详述. 这个box实际上看不见, 它只是用来当成是个工具来安排物件 */ box1 = gtk_hbox_new(FALSE, 0); /* 将box放到主视窗中. */ gtk_container_add (GTK_CONTAINER (window), box1); /* 产生一个新按钮并带有标签"Button 1". */ button = gtk_button_new_with_label ("Button 1"); /* 当按钮被按下的时候, 我们呼叫"callback"函数 * 并以其指标做为参数送到"button 1" */ gtk_signal_connect (GTK_OBJECT (button), "clicked", GTK_SIGNAL_FUNC (callback), (gpointer) "button 1"); /* instead of gtk_container_add, we pack this button into the invisible * box, which has been packed into the window. */ gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0); /* always remember this step, this tells GTK that our preparation for * this button is complete, and it can be displayed now. */ gtk_widget_show(button); /* do these same steps again to create a second button */ button = gtk_button_new_with_label ("Button 2"); /* call the same callback function with a different argument, * passing a pointer to "button 2" instead. */ gtk_signal_connect (GTK_OBJECT (button), "clicked", GTK_SIGNAL_FUNC (callback), (gpointer) "button 2"); gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0); /* The order in which we show the buttons is not really important, but I * recommend showing the window last, so it all pops up at once. */ gtk_widget_show(button); gtk_widget_show(box1); gtk_widget_show (window); /* rest in gtk_main and wait for the fun to begin! */ gtk_main (); return 0;}</PRE></CODE></BLOCKQUOTE><P>将这个程式以相同的参数编译, 您会看到没有任何方法来离开程式, 您必须使用视窗管理程式或命令列来杀掉它.对读者来说, 加个"Quit"按钮会是个不错的练习.您也可以玩一玩gtk_box_pack_start()这个东西. 试试拉一拉视窗, 看看有什麽变换.<P>另外有个蛮有用的define给gtk_window_new()用 - GTK_WINDOW_DIALOG.它的互动行为有点不太一样.<P><HR><A HREF="gtk_tut-2.html" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut-2.html"><IMG SRC="prev.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/prev.gif" ALT="Previous"></A><A HREF="gtk_tut-4.html" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut-4.html"><IMG SRC="next.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/next.gif" ALT="Next"></A><A HREF="gtk_tut.html#toc3" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/gtk_tut.html#toc3"><IMG SRC="toc.gif" tppabs="http://extend.hk.hi.cn/%7ehusuyu/http/beginner/gtk/toc.gif" ALT="Contents"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -