⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sec-steppingthroughhelloworld.html

📁 gtk 开发手册和参考文档。 包括gtk glib gdk等
💻 HTML
字号:
<HTML><HEAD><TITLE>Stepping Through Hello World</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="Events"HREF="sec-events.html"><LINKREL="NEXT"TITLE="Moving On"HREF="ch-movingon.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-events.html">&#60;&#60;&#60; Previous</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 2. Getting Started</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="ch-movingon.html">Next &#62;&#62;&#62;</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SEC-STEPPINGTHROUGHHELLOWORLD">2.5. Stepping Through Hello World</A></H1><P>Now that we know the theory behind this, let's clarify by walkingthrough the example <ICLASS="EMPHASIS">helloworld</I> program.</P><P>Here is the callback function that will be called when the button is"clicked". We ignore both the widget and the data in this example, butit is not hard to do things with them. The next example will use thedata argument to tell us which button was pressed.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void hello( GtkWidget *widget,            gpointer   data ){    g_print ("Hello World\n");}</PRE></TD></TR></TABLE><P>The next callback is a bit special. The "delete_event" occurs when thewindow manager sends this event to the application. We have a choicehere as to what to do about these events. We can ignore them, makesome sort of response, or simply quit the application.</P><P>The value you return in this callback lets GTK know what action totake.  By returning TRUE, we let it know that we don't want to havethe "destroy" signal emitted, keeping our application running. Byreturning FALSE, we ask that "destroy" be emitted, which in turn willcall our "destroy" signal handler.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">gint delete_event( GtkWidget *widget,                   GdkEvent  *event,		   gpointer   data ){    g_print ("delete event occurred\n");    return (TRUE); }</PRE></TD></TR></TABLE><P>Here is another callback function which causes the program to quit bycalling gtk_main_quit(). This function tells GTK that it is to exitfrom gtk_main when control is returned to it.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void destroy( GtkWidget *widget,              gpointer   data ){    gtk_main_quit ();}</PRE></TD></TR></TABLE><P>I assume you know about the main() function... yes, as with otherapplications, all GTK applications will also have one of these.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">int main( int   argc,          char *argv[] ){</PRE></TD></TR></TABLE><P>This next part declares pointers to a structure of typeGtkWidget. These are used below to create a window and a button.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">    GtkWidget *window;    GtkWidget *button;</PRE></TD></TR></TABLE><P>Here is our gtk_init again. As before, this initializes the toolkit,and parses the arguments found on the command line. Any argument itrecognizes from the command line, it removes from the list, andmodifies argc and argv to make it look like they never existed,allowing your application to parse the remaining arguments.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">    gtk_init (&#38;argc, &#38;argv);</PRE></TD></TR></TABLE><P>Create a new window. This is fairly straightforward. Memory isallocated for the GtkWidget *window structure so it now points to avalid structure. It sets up a new window, but it is not displayeduntil we call gtk_widget_show(window) near the end of our program.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);</PRE></TD></TR></TABLE><P>Here are two examples of connecting a signal handler to an object, inthis case, the window. Here, the "delete_event" and "destroy" signalsare caught. The first is emitted when we use the window manager tokill the window, or when we use the gtk_widget_destroy() call passingin the window widget as the object to destroy. The second is emittedwhen, in the "delete_event" handler, we return FALSE. The <TTCLASS="LITERAL">GTK_OBJECT</TT> and <TTCLASS="LITERAL">GTK_SIGNAL_FUNC</TT> are macros that performtype casting and checking for us, as well as aid the readability ofthe code.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">    gtk_signal_connect (GTK_OBJECT (window), "delete_event",                        GTK_SIGNAL_FUNC (delete_event), NULL);    gtk_signal_connect (GTK_OBJECT (window), "destroy",                        GTK_SIGNAL_FUNC (destroy), NULL);</PRE></TD></TR></TABLE><P>This next function is used to set an attribute of a container object.This just sets the window so it has a blank area along the inside ofit 10 pixels wide where no widgets will go. There are other similarfunctions which we will look at in the section on<AHREF="ch-settingwidgetattributes.html">Setting Widget Attributes</A></P><P>And again, <TTCLASS="LITERAL">GTK_CONTAINER</TT> is a macro to perform type casting.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">    gtk_container_set_border_width (GTK_CONTAINER (window), 10);</PRE></TD></TR></TABLE><P>This call creates a new button. It allocates space for a new GtkWidgetstructure in memory, initializes it, and makes the button pointerpoint to it. It will have the label "Hello World" on it whendisplayed.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">    button = gtk_button_new_with_label ("Hello World");</PRE></TD></TR></TABLE><P>Here, we take this button, and make it do something useful. We attacha signal handler to it so when it emits the "clicked" signal, ourhello() function is called. The data is ignored, so we simply pass inNULL to the hello() callback function. Obviously, the "clicked" signalis emitted when we click the button with our mouse pointer.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">    gtk_signal_connect (GTK_OBJECT (button), "clicked",                        GTK_SIGNAL_FUNC (hello), NULL);</PRE></TD></TR></TABLE><P>We are also going to use this button to exit our program. This willillustrate how the "destroy" signal may come from either the windowmanager, or our program. When the button is "clicked", same as above,it calls the first hello() callback function, and then this one in theorder they are set up. You may have as many callback functions as youneed, and all will be executed in the order you connectedthem. Because the gtk_widget_destroy() function accepts only aGtkWidget *widget as an argument, we use thegtk_signal_connect_object() function here instead of straightgtk_signal_connect().</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">    gtk_signal_connect_object (GTK_OBJECT (button), "clicked",                               GTK_SIGNAL_FUNC (gtk_widget_destroy),                               GTK_OBJECT (window));</PRE></TD></TR></TABLE><P>This is a packing call, which will be explained in depth later on in<AHREF="ch-packingwidgets.html">Packing Widgets</A>. But it isfairly easy to understand. It simply tells GTK that the button is tobe placed in the window where it will be displayed. Note that a GTKcontainer can only contain one widget. There are other widgets, thatare described later, which are designed to layout multiple widgets invarious ways. </P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">    gtk_container_add (GTK_CONTAINER (window), button);</PRE></TD></TR></TABLE><P>Now we have everything set up the way we want it to be. With all thesignal handlers in place, and the button placed in the window where itshould be, we ask GTK to "show" the widgets on the screen. The windowwidget is shown last so the whole window will pop up at once ratherthan seeing the window pop up, and then the button form inside ofit. Although with such a simple example, you'd never notice.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">    gtk_widget_show (button);    gtk_widget_show (window);</PRE></TD></TR></TABLE><P>And of course, we call gtk_main() which waits for events to come fromthe X server and will call on the widgets to emit signals when theseevents come.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">    gtk_main ();</PRE></TD></TR></TABLE><P>And the final return. Control returns here after gtk_quit() is called.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">    return (0);</PRE></TD></TR></TABLE><P>Now, when we click the mouse button on a GTK button, the widget emitsa "clicked" signal. In order for us to use this information, ourprogram sets up a signal handler to catch that signal, whichdispatches the function of our choice. In our example, when the buttonwe created is "clicked", the hello() function is called with a NULLargument, and then the next handler for this signal is called. Thiscalls the gtk_widget_destroy() function, passing it the window widgetas its argument, destroying the window widget. This causes the windowto emit the "destroy" signal, which is caught, and calls our destroy()callback function, which simply exits GTK.</P><P>Another course of events is to use the window manager to kill thewindow, which will cause the "delete_event" to be emitted. This willcall our "delete_event" handler. If we return TRUE here, the windowwill be left as is and nothing will happen. Returning FALSE will causeGTK to emit the "destroy" signal which of course calls the "destroy"callback, exiting GTK.</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-events.html">&#60;&#60;&#60; Previous</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="gtk-tut.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="ch-movingon.html">Next &#62;&#62;&#62;</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Events</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ch-gettingstarted.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Moving On</TD></TR></TABLE></DIV>        </td>    </tr></table>  </td>  </tr></table></body></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -