?? sec-widgetindetail.html
字號:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html> <head> <title> GtkWidget In Detail </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="Writing a GtkWidget" href= "cha-widget.html"> <link rel="PREVIOUS" title="An Example: The GtkEv Widget" href= "z147.html"> <link rel="NEXT" title="GtkVBox: A Windowless Container" href= "z166.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="z147.html"><font color="#0000ff" size="2"><b> <<< 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="z166.html"><font color="#0000ff" size="2"><b> Next >>></b></font></a> </td> </tr> </table> </div> <div class="SECT1"> <h1 class="SECT1"> <a name="SEC-WIDGETINDETAIL"><tt class="CLASSNAME"> GtkWidget</tt> In Detail</a> </h1> <p> This section catalogs the functions in <span class= "STRUCTNAME">GtkWidgetClass</span> more rigorously, and describes the default implementation of each. </p> <div class="SECT2"> <h2 class="SECT2"> <a name="Z155">Destruction</a> </h2> <p> Widget destruction is not significantly different from object destruction in general, as described in <a href= "sec-finalization.html">the section called <i>Object Finalization</i> in the chapter called <i>The GTK+ Object and Type System</i></a>. As always, there are three phases: shutdown, destroy, and finalize. Only the destroy method is a signal; the others are class functions only. If you override any of the three, you must "chain up" to the parent class implementation. </p> <p> <tt class="CLASSNAME">GtkWidget</tt> has default implementations of all three; you should know what they do: </p> <ul> <li> <p> In its shutdown method, a <tt class="CLASSNAME"> GtkWidget</tt> removes itself from its parent container (if any), and then unrealizes itself. This implies that widgets are always unrealized inside their destroy methods. It chains up to the <span class="STRUCTNAME">GtkObject</span> shutdown method which emits the <span class="SYMBOL">"destroy"</span> signal. </p> </li> <li> <p> In its destroy method, a <tt class="CLASSNAME"> GtkWidget</tt> releases the grab if it has it, unreferences its style and sets <span class= "STRUCTNAME">widget->style</span> to <span class= "STRUCTNAME">NULL</span>, and gives up any connections it had to the current selection. (<span class="STRUCTNAME">GtkEntry</span> and other editable-text widgets allow you to select and paste text.) It chains up to the <span class="STRUCTNAME"> GtkObject</span> destroy method which disconnects any signal handlers for the object. </p> </li> <li> <p> In its finalize method, a <tt class="CLASSNAME"> GtkWidget</tt> frees a number of private data structures (stored as object data, see <a href= "sec-objectdata.html">the section called <i>Attaching Data to Objects</i> in the chapter called <i>The GTK+ Object and Type System</i></a>), and frees <span class="STRUCTNAME">widget->name</span>. It chains up to the <span class="STRUCTNAME">GtkObject</span> finalize method which frees the instance struct. </p> </li> </ul> </div> <div class="SECT2"> <h2 class="SECT2"> <a name="Z156">Showing, Hiding, and Mapping</a> </h2> <p> Four methods are provided to show and hide widgets. <span class="STRUCTNAME">show</span> and <span class= "STRUCTNAME">hide</span> are rarely overridden; the default implementations almost always suffice. <span class="STRUCTNAME">show_all</span> and <span class= "STRUCTNAME">hide_all</span> are overridden by container widgets; they show or hide the container and all its children. Their default implementation simply shows the widget itself: no non-container should need to change this. <span class="SYMBOL">"show"</span> and <span class= "SYMBOL">"hide"</span> are registered as signals, but the <span class="STRUCTNAME">_all</span> variants are not. </p> <p> The default implementation of <span class="STRUCTNAME"> show</span> sets the <span class="STRUCTNAME"> GTK_VISIBLE</span> flag, and maps the widget if its parent is mapped. When containers are mapped, they map any children with the <span class="STRUCTNAME"> GTK_VISIBLE</span> flag set. Thus, the <span class= "STRUCTNAME">show</span> implementation ensures that a widget will be mapped eventually. (When a widget is finally mapped, the <span class="STRUCTNAME"> GTK_MAPPED</span> flag is set in addition to <span class= "STRUCTNAME">GTK_VISIBLE</span>.) </p> <p> The default <span class="STRUCTNAME">hide</span> implementation does the opposite: it unsets the <span class="STRUCTNAME">GTK_VISIBLE</span> flag, and unmaps the widget if <span class="STRUCTNAME">GTK_MAPPED</span> is set. </p> <p> The default <span class="STRUCTNAME">map</span> and <span class="STRUCTNAME">unmap</span> implementations are much more frequently overridden. The default implementations should suffice for windowless widgets and widgets with a single window (<span class= "STRUCTNAME">widget->window</span>). Widgets with additional subwindows or other special needs may need to override the defaults. </p> <p> The map method is responsible for putting a widget on the screen. The default implementation sets the <span class= "STRUCTNAME">GTK_MAPPED</span> flag, and calls <tt class= "FUNCTION">gdk_window_show()</tt> on <span class= "STRUCTNAME">widget->window</span> for widgets that have a window. If a widget has subwindows, or needs to take any special action when it appears on the screen, it must override the map method. (It may optionally chain up to the default method, however.) Container widgets are required to override the map method, because they must iterate over their children and map each child widget with the <span class="STRUCTNAME">GTK_VISIBLE</span> flag set (i.e., each child widget that's been shown). </p> <p> The unmap method is simply the reverse of the map method; it undoes anything the map method did. By default, it unsets the <span class="STRUCTNAME">GTK_MAPPED</span> flag and calls <tt class="FUNCTION"> gdk_window_hide()</tt> to hide <span class="STRUCTNAME"> widget->window</span>. Container widgets must override the method to unmap their child windows. </p> <p> Keep in mind that none of these methods are invoked directly; they are invoked by calling <tt class= "FUNCTION">gtk_widget_show()</tt>, <tt class="FUNCTION"> gtk_widget_map()</tt>, and so on. These functions may perform special actions before and after invocation. Here is a summary: </p> <ul> <li> <p> <tt class="FUNCTION">gtk_widget_show()</tt> "queues a resize" on the widget before emitting the show signal. This means notifying the widget's parent container that it should rearrange its layout. </p> </li> <li> <p> <tt class="FUNCTION">gtk_widget_hide()</tt> does the same, since a newly-invisible widget implies recalculating the layout just as a newly-visible one does. </p> </li> <li> <p> <tt class="FUNCTION">gtk_widget_show_all()</tt> and <tt class="FUNCTION">gtk_widget_hide_all()</tt> don't do anything special, they simply invoke the corresponding class function. </p> </li> <li> <p> <tt class="FUNCTION">gtk_widget_map()</tt> realizes the widget before emitting the <span class="SYMBOL"> "map"</span> signal, if the widget is not realized. This maintains an important invariant (all mapped widgets are also realized). After emitting the signal, <tt class="FUNCTION">gtk_widget_map()</tt> queues a draw for windowless widgets; widgets with windows will receive an expose event when the window appears on the screen, so queueing a draw is not necessary. </p> </li> <li> <p> <tt class="FUNCTION">gtk_widget_unmap()</tt> redraws part of the parent widget if a windowless child is unmapped (remember that windowless widgets draw on their parent's <span class="STRUCTNAME"> widget->window</span>). </p> </li> </ul> </div> <div class="SECT2"> <h2 class="SECT2"> <a name="SEC-REALIZEMETHOD">Realization</a> </h2> <p> The <span class="STRUCTNAME">realize</span> and <span class="STRUCTNAME">unrealize</span> class functions are registered as signals. <i class="FIRSTTERM"> Realization</i> is the process of creating GDK resources associated with the widget; including but not limited to <span class="STRUCTNAME">widget->window</span> and <span class="STRUCTNAME">widget->style</span>. </p> <p> A realize method should do the following: </p> <ul> <li> <p> Set the <span class="STRUCTNAME">GTK_REALIZED</span> flag. </p> </li> <li> <p> Create the widget's windows, especially <span class= "STRUCTNAME">widget->window</span> which should be a child of the widget's parent's <span class= "STRUCTNAME">widget->window</span> (obtained with <tt class="FUNCTION"> gtk_widget_get_parent_window()</tt>). </p> </li> <li> <p> Place a pointer to the widget in the user data field of each window. </p> </li> <li> <p> For windowless widgets, <span class="STRUCTNAME"> widget->window</span> should be set to the parent widget's window (obtained with <tt class="FUNCTION"> gtk_widget_get_parent_window()</tt>). These widgets should also increase the reference count on <span class="STRUCTNAME">widget->window</span> by calling <tt class="FUNCTION">gdk_window_ref()</tt>. </p> </li> <li> <p> Set <span class="STRUCTNAME">widget->style</span> using <tt class="FUNCTION">gtk_style_attach()</tt>. </p> </li> <li> <p> Set the background of each window using <tt class= "FUNCTION">gtk_style_set_background()</tt> if possible, and failing that using some color from the style. A windowless widget should not do this, since its parent already has. </p> </li> </ul> <p> The default implementation of <span class="STRUCTNAME"> realize</span> is only appropriate for windowless widgets; it sets the <span class="STRUCTNAME"> GTK_REALIZED</span> flag, sets <span class="STRUCTNAME"> widget->window</span> to the parent widget's window, increases the reference count on the parent's window, and sets <span class="STRUCTNAME">widget->style</span>. Widgets with their own <span class="STRUCTNAME"> GdkWindow</span> must override the realize method. </p> <p> The <span class="SYMBOL">"realize"</span> signal invokes the realize method as its default handler. This signal should never be emitted directly, because there are substantial pre- and post-conditions to be enforced. <tt class="FUNCTION">gtk_widget_realize()</tt> takes care of the details. Among other things, it ensures that the widget's parent is realized; GTK+ maintains the invariant that widgets cannot be realized unless their parents are also realized. </p>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -