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

📄 z57.html

📁 GTK+_ Gnome Application Development
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>  <head>    <title>      Widget Concepts    </title>    <meta name="GENERATOR" content=    "Modular DocBook HTML Stylesheet Version 1.45">    <link rel="HOME" title="GTK+ / Gnome Application Development"    href="ggad.html">    <link rel="UP" title="GTK+ Basics" href="cha-gtk.html">    <link rel="PREVIOUS" title="Containers And Widget Layout" href=     "sec-containers.html">    <link rel="NEXT" title="The Main Loop" href=    "sec-mainloop.html">  </head>  <body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink=   "#840084" alink="#0000FF">    <div class="NAVHEADER">      <table width="100%" border="0" bgcolor="#ffffff" cellpadding=       "1" cellspacing="0">        <tr>          <th colspan="4" align="center">            <font color="#000000" size="2">GTK+ / Gnome Application            Development</font>          </th>        </tr>        <tr>          <td width="25%" bgcolor="#ffffff" align="left">            <a href="sec-containers.html"><font color="#0000ff"            size="2"><b>&lt;&lt;&lt; Previous</b></font></a>          </td>          <td width="25%" colspan="2" bgcolor="#ffffff" align=           "center">            <font color="#0000ff" size="2"><b><a href="ggad.html">            <font color="#0000ff" size="2"><b>            Home</b></font></a></b></font>          </td>          <td width="25%" bgcolor="#ffffff" align="right">            <a href="sec-mainloop.html"><font color="#0000ff" size=             "2"><b>Next &gt;&gt;&gt;</b></font></a>          </td>        </tr>      </table>    </div>    <div class="SECT1">      <h1 class="SECT1">        <a name="Z57">Widget Concepts</a>      </h1>      <p>        This section discusses concepts that apply to all widgets,        including memory management and certain special states        widgets can be in. It's a "conceptual" section; however,        the concepts are very important to practical topics covered        later in the book.      </p>      <div class="SECT2">        <h2 class="SECT2">          <a name="WIDGETLIFECYCLE">Widget Life Cycle</a>        </h2>        <p>          Widget resource and memory management is mostly          automatic. However, there are a couple of "gotchas" to          keep in mind if you're doing more complicated things.        </p>        <div class="FIGURE">          <a name="FL-WIDGETDESTROY"></a>          <div class="FUNCSYNOPSIS">            <a name="FL-WIDGETDESTROY.SYNOPSIS"></a>            <table border="0" bgcolor="#E0E0E0" width="100%">              <tr>                <td><pre class="FUNCSYNOPSISINFO">#include &lt;gtk/gtkwidget.h&gt;</pre>                </td>              </tr>            </table>            <p>              <code><code class="FUNCDEF">void <tt class=              "FUNCTION">gtk_widget_destroy</tt></code>(GtkWidget*              <tt class="PARAMETER"><i>widget</i></tt>);</code>            </p>          </div>          <p>            <b>Figure 21. Widget Destruction</b>          </p>        </div>        <p>          A widget can be destroyed at any time by calling <tt          class="FUNCTION">gtk_widget_destroy()</tt> (shown in <a          href="z57.html#FL-WIDGETDESTROY">Figure 21</a>);          destroying a widget frees any associated memory and other          resources. If the widget is inside a container, it is          automatically removed from the container before it's          destroyed. It's worth noting that <tt class="FUNCTION">          gtk_widget_destroy()</tt> is simply another name for <tt          class="FUNCTION">gtk_object_destroy()</tt>; <span class=           "STRUCTNAME">GtkObject</span>s have "virtual destructors"          so <tt class="FUNCTION">gtk_object_destroy()</tt> will          always do the right thing.        </p>        <p>          Internally, a reference count is maintained for all          widgets (actually, all <span class="STRUCTNAME">          GtkObject</span>s). Objects begin their life with a          reference count of 1, even though they have not yet been          referenced. At this stage the object is said to be <i          class="FIRSTTERM">floating</i> and is flagged as such. It          is possible to remove the object's initial reference;          this is called <i class="FIRSTTERM">sinking</i> the          floating object and will destroy the object if the          floating reference was the only one.        </p>        <p>          Containers first reference and then sink any floating          widgets that are added to them. By sinking a widget, a          container "takes ownership" of it for resource management          purposes. Thus, the reference count of the widget remains          1, but the object is no longer flagged as floating. When          a widget is removed from a container---or the container          is destroyed---the reference count is decremented to 0.          When an object's reference count reaches 0, it is          destroyed.        </p>        <p>          In practice, this means that you only have to destroy          toplevel widgets; any widgets that are inside a container          will be destroyed along with the container.        </p>        <p>          There's a danger here, however. Sometimes you want to          remove a widget from a container; perhaps some element of          your interface is optional or only appears under certain          circumstances. When you remove the widget (using <tt          class="FUNCTION">gtk_container_remove()</tt>), it will be          unreferenced, its reference count will drop to 0, and it          will be destroyed. To avoid this situation, you should          add a reference to the widget before you remove it. <a          href="z57.html#FL-REFCOUNTS">Figure 22</a> lists the          functions to manipulate reference counts.        </p>        <div class="FIGURE">          <a name="FL-REFCOUNTS"></a>          <div class="FUNCSYNOPSIS">            <a name="FL-REFCOUNTS.SYNOPSIS"></a>            <table border="0" bgcolor="#E0E0E0" width="100%">              <tr>                <td><pre class="FUNCSYNOPSISINFO">#include &lt;gtk/gtkobject.h&gt;</pre>                </td>              </tr>            </table>            <p>              <code><code class="FUNCDEF">void <tt class=              "FUNCTION">gtk_object_ref</tt></code>(GtkObject* <tt              class="PARAMETER"><i>object</i></tt>);</code>            </p>            <p>              <code><code class="FUNCDEF">void <tt class=              "FUNCTION">gtk_object_unref</tt></code>(GtkObject*              <tt class="PARAMETER"><i>object</i></tt>);</code>            </p>            <p>              <code><code class="FUNCDEF">void <tt class=              "FUNCTION">gtk_object_sink</tt></code>(GtkObject* <tt              class="PARAMETER"><i>object</i></tt>);</code>            </p>          </div>          <p>            <b>Figure 22. Reference Counting</b>          </p>        </div>        <p>          <tt class="FUNCTION">gtk_object_ref()</tt> and <tt class=           "FUNCTION">gtk_object_unref()</tt> have widget-specific          variants (<tt class="FUNCTION">gtk_widget_ref()</tt>,          etc.) but the object and widget versions are completely          synonymous. The widget-specific versions are leftovers          from earlier versions of GTK+.        </p>        <p>          So to safely remove a widget from a container, you might          do this:        </p>        <table border="0" bgcolor="#E0E0E0" width="100%">          <tr>            <td><pre class="PROGRAMLISTING">&#13;  gtk_widget_ref(widget);  gtk_container_remove(container, widget);&#13;</pre>            </td>          </tr>        </table>        <p>          The widget now has one reference, held by your code. At          some point you'll need to release the reference,          destroying the widget. (It would make sense to do so          after re-adding the widget to some other container, for          example.)        </p>        <p>          It's worth pointing out that removing widgets from          containers is uncommon; in general it's faster to simply          hide the widget with <tt class="FUNCTION">          gtk_widget_hide()</tt>, then <tt class="FUNCTION">          gtk_widget_show()</tt> it at some later time.        </p>        <p>          <tt class="FUNCTION">gtk_object_sink()</tt> is used          almost exclusively in widget implementations, when you          expect to be the primary "owner" of an object. If an          object is not "floating", <tt class="FUNCTION">          gtk_object_sink()</tt> has no effect. To claim ownership          of a widget, do this:        </p>        <table border="0" bgcolor="#E0E0E0" width="100%">          <tr>            <td><pre class="PROGRAMLISTING">&#13;  gtk_widget_ref(widget);  gtk_object_sink(GTK_OBJECT(widget));&#13;</pre>            </td>          </tr>        </table>        <p>          This code adds one reference to the widget; if the widget          was "floating," it also subtracts one reference. If the          widget was not floating, <tt class="FUNCTION">          gtk_widget_sink()</tt> has no effect.        </p>        <p>          It's important to understand the details because in some          cases they can be important. But most of the time, you          can get by with a few simple rules:        </p>        <ul>          <li>            <p>              You must destroy any toplevel widgets when you are              done with them, but child widgets are destroyed              automatically.&#13;            </p>          </li>          <li>            <p>              If you want to remove a widget from a container              without destroying it, you must first add a reference              to the widget.&#13;            </p>          </li>          <li>            <p>

⌨️ 快捷键说明

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