📄 sec-theoryofsignalsandcallbacks.html
字号:
<HTML><HEAD><TITLE>Theory of Signals and Callbacks</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.49"><LINKREL="HOME"TITLE="GTK+ 1.2 Tutorial"HREF="gtk-tut.html"><LINKREL="UP"TITLE="Getting Started"HREF="ch-gettingstarted.html"><LINKREL="PREVIOUS"TITLE="Compiling Hello World"HREF="sec-compiling.html"><LINKREL="NEXT"TITLE="Events"HREF="sec-events.html"></HEAD></head><body bgcolor="#FFFFFF" marginheight=0 marginwidth=0 width="100%"><table cellspacing=6 border=0 cellpadding=0 width="100%"> <tr> <td bgcolor="#FFFFFF" valign=top nowrap> <centeR><A HREF="/"><img src="/images/gtk-logo-rgb.gif" width=107 height=140 border=0></A> <font face="helvetica,lucidia" color="#000000"><BR><BR><b>GTK+<BR>The GIMP Toolkit</b></center><BR><table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>General</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/">Introduction</A><BR><A HREF="/screenshots/">Screenshots</A><br><A HREF="/download/">Download</A><br><A HREF="/mailinglists.html">Mailing Lists</A><BR><A HREF="/bindings.html">Language Bindings</A><BR><A HREF="http://gtk.themes.org/">Themes</A><BR><A HREF="/bugs.html">Bug Tracker</A><BR></td></tr></table></td></tr></table><BR> <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Documentation</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/faq/">FAQ</A><br><A HREF="/tutorial/">Tutorial</A><BR><A HREF="/api/">API Reference</A><br><A HREF="/books.html">Published Books</A><BR></td></tr></table></td></tr></table><BR> <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Projects</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.pango.org/">Pango</A><BR><A HREF="http://sources.redhat.com/inti/">Inti</A><BR><A HREF="http://www.gnome.org/">GNOME</A><BR><A HREF="http://user.sgic.fi/~tml/gimp/win32/">GTK+ for Win32</A><br><A HREF="http://people.redhat.com/sopwith/gtkfb/">GtkFB (Framebuffer)</A><br><A HREF="http://www.directfb.org/gtk.xml">GTK+ on DirectFB</A><BR><A HREF="/beos/">GTK+ for BeOS</A></td></tr></table></td></tr></table><BR> <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B><B>Applications</B></B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.gimp.org/">GIMP</A><BR><A HREF="http://www.abiword.org/">Abiword</A><BR><A HREF="http://www.lysator.liu.se/~alla/dia/dia.html">Dia</A><BR><A HREF="http://glade.pn.org/">Glade</A><BR><A HREF="http://www.gnucash.org/">GnuCash</A><BR><A HREF="http://www.gnome.org/projects/gnumeric/">Gnumeric</A><BR><BR><A HREF="http://www.gnome.org/applist/">GNOME Software Map</A><br></td></tr></table></td></tr></table><BR> </td> <td bgcolor="#ffffff" valign=top width="99%"><font face="lucida,helvetica"><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GTK+ 1.2 Tutorial</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="sec-compiling.html"><<< Previous</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 2. Getting Started</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="sec-events.html">Next >>></A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SEC-THEORYOFSIGNALSANDCALLBACKS">2.3. Theory of Signals and Callbacks</A></H1><P>Before we look in detail at <ICLASS="EMPHASIS">helloworld</I>, we'll discuss signalsand callbacks. GTK is an event driven toolkit, which means it willsleep in gtk_main until an event occurs and control is passed to theappropriate function.</P><P>This passing of control is done using the idea of "signals". (Notethat these signals are not the same as the Unix system signals, andare not implemented using them, although the terminology is almostidentical.) When an event occurs, such as the press of a mouse button,the appropriate signal will be "emitted" by the widget that waspressed. This is how GTK does most of its useful work. There aresignals that all widgets inherit, such as "destroy", and there aresignals that are widget specific, such as "toggled" on a togglebutton.</P><P>To make a button perform an action, we set up a signal handler tocatch these signals and call the appropriate function. This is done byusing a function such as:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">gint gtk_signal_connect( GtkObject *object, gchar *name, GtkSignalFunc func, gpointer func_data );</PRE></TD></TR></TABLE><P>where the first argument is the widget which will be emitting thesignal, and the second the name of the signal you wish to catch. Thethird is the function you wish to be called when it is caught, and thefourth, the data you wish to have passed to this function.</P><P>The function specified in the third argument is called a "callbackfunction", and should generally be of the form</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void callback_func( GtkWidget *widget, gpointer callback_data );</PRE></TD></TR></TABLE><P>where the first argument will be a pointer to the widget that emittedthe signal, and the second a pointer to the data given as the lastargument to the gtk_signal_connect() function as shown above.</P><P>Note that the above form for a signal callback function declaration isonly a general guide, as some widget specific signals generatedifferent calling parameters. For example, the CList "select_row"signal provides both row and column parameters.</P><P>Another call used in the <ICLASS="EMPHASIS">helloworld</I> example, is:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">gint gtk_signal_connect_object( GtkObject *object, gchar *name, GtkSignalFunc func, GtkObject *slot_object );</PRE></TD></TR></TABLE><P>gtk_signal_connect_object() is the same as gtk_signal_connect() exceptthat the callback function only uses one argument, a pointer to a GTKobject. So when using this function to connect signals, the callbackshould be of the form</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void callback_func( GtkObject *object );</PRE></TD></TR></TABLE><P>where the object is usually a widget. We usually don't setup callbacksfor gtk_signal_connect_object however. They are usually used to call aGTK function that accepts a single widget or object as an argument, asis the case in our <ICLASS="EMPHASIS">helloworld</I> example.</P><P>The purpose of having two functions to connect signals is simply toallow the callbacks to have a different number of arguments. Manyfunctions in the GTK library accept only a single GtkWidget pointer asan argument, so you want to use the gtk_signal_connect_object() forthese, whereas for your functions, you may need to have additionaldata supplied to the callbacks.</P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="sec-compiling.html"><<< Previous</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="gtk-tut.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="sec-events.html">Next >>></A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Compiling Hello World</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ch-gettingstarted.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Events</TD></TR></TABLE></DIV> </td> </tr></table> </td> </tr></table></body></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -