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

📄 sec-classinit.html

📁 GTK+_ Gnome Application Development
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html>  <head>    <title>      Initializing a New Class    </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="The GTK+ Object and Type System" href=     "cha-objects.html">    <link rel="PREVIOUS" title="Type Checking and New Types" href=     "z105.html">    <link rel="NEXT" title="GtkArg and the Type System" href=     "sec-gtkarg.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="z105.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-gtkarg.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="SEC-CLASSINIT">Initializing a New Class</a>      </h1>      <p>        When a type is first used, GTK+ creates an instance of its        class struct (using the information supplied to <tt class=         "FUNCTION">gtk_type_unique()</tt>). To initialize the class        struct for a type, GTK+ first checks that all parent        classes are initialized and initializes them if not. Then        it fills the top portion of the class struct with a        byte-for-byte copy of the parent's class struct. This means        the subclass inherits any function pointers found in the        parent class.      </p>      <p>        Next, the base class initialization functions of each        parent class and that of the class itself are called in        order, starting with <span class="STRUCTNAME">        GtkObject</span>. (The base class init function is the last        argument to <tt class="FUNCTION">gtk_type_unique()</tt>). A        base class initializer is optional; in the <tt class=         "CLASSNAME">GtkButton</tt> case, there is none. If present,        the base class initializer supplements the byte-for-byte        copy of the class struct; for example, some functions        should not be inherited. To prevent class function        inheritance, the base class initializer can zero certain        function pointers. Normally you do not need a base class        initializer.      </p>      <p>        Finally, GTK+ calls the type's own class init function. The        class init function can override functions from the parent        class by replacing them in the class struct. It should also        fill in any functions unique to the subclass, and register        signals and object arguments (discussed later in the        chapter).      </p>      <p>        A concrete example should make the class creation process        clear. The class hierarchy for <tt class="CLASSNAME">        GtkButton</tt> is shown in <a href=         "sec-classinit.html#FIG-GTKBUTTON">Figure 1</a>. When the        <tt class="CLASSNAME">GtkButton</tt> type is registered, an        empty <tt class="CLASSNAME">GtkButtonClass</tt> is created.        This class struct is initialized as follows:      </p>      <ol type="1">        <li>          <p>            The class struct for <tt class="CLASSNAME">GtkBin</tt>,            <tt class="CLASSNAME">GtkButton</tt>'s immediate            parent, is copied into it. This means <tt class=             "CLASSNAME">GtkButton</tt> inherits class functions            from <tt class="CLASSNAME">GtkBin</tt>.&#13;          </p>        </li>        <li>          <p>            The base class initialization function for <span class=             "STRUCTNAME">GtkObject</span> is called on it. This            zeroes some <span class="STRUCTNAME">GtkObject</span>            class functions that should not be inherited.&#13;          </p>        </li>        <li>          <p>            There is no base class initializer for <tt class=             "CLASSNAME">GtkWidget</tt>, or it would be called.            &#13;          </p>        </li>        <li>          <p>            The base class initializer for <tt class="CLASSNAME">            GtkContainer</tt> is called. This zeroes some <span            class="STRUCTNAME">GtkContainer</span> class functions            that should not be inherited, and initializes a <span            class="STRUCTNAME">GtkContainerClass</span> data            member.&#13;          </p>        </li>        <li>          <p>            There is no base class initializer for <tt class=             "CLASSNAME">GtkBin</tt>, or it would be called.&#13;          </p>        </li>        <li>          <p>            There is no base class initializer for <tt class=             "CLASSNAME">GtkButton</tt>, or it would be called.&#13;          </p>        </li>        <li>          <p>            The class initializer is called for <tt class=            "CLASSNAME">GtkButton</tt>. This fills in the <span            class="STRUCTNAME">GtkButtonClass</span> structure,            registers signals, and registers object arguments.&#13;          </p>        </li>      </ol>      <p>        When writing a new class, you only need to concern yourself        with the final two steps---you should consider whether a        base class initializer is needed, and supply it if so; you        must supply a class initializer in all cases.      </p>      <div class="FIGURE">        <a name="FIG-GTKBUTTON"></a>        <p>          <b>Figure 1. <tt class="CLASSNAME">GtkButton</tt>          Ancestry</b>        </p>      </div>      <table border="0" bgcolor="#E0E0E0" width="100%">        <tr>          <td><pre class="PROGRAMLISTING">&#13;GtkObject   |GtkWidget   | GtkContainer   | GtkBin   | GtkButton&#13;</pre>          </td>        </tr>      </table>      <p>        Here is the <tt class="CLASSNAME">GtkButton</tt> class        initialization function, just to give you an initial sense        of things; read on to learn what this code does.      </p>      <table border="0" bgcolor="#E0E0E0" width="100%">        <tr>          <td><pre class="PROGRAMLISTING">&#13;static voidgtk_button_class_init (GtkButtonClass *klass){  GtkObjectClass *object_class;  GtkWidgetClass *widget_class;  GtkContainerClass *container_class;  object_class = (GtkObjectClass*) klass;  widget_class = (GtkWidgetClass*) klass;  container_class = (GtkContainerClass*) klass;  parent_class = gtk_type_class (GTK_TYPE_BIN);  gtk_object_add_arg_type ("GtkButton::label",                            GTK_TYPE_STRING,                            GTK_ARG_READWRITE,                            ARG_LABEL);  gtk_object_add_arg_type ("GtkButton::relief",                            GTK_TYPE_RELIEF_STYLE,                            GTK_ARG_READWRITE,                            ARG_RELIEF);  button_signals[PRESSED] =    gtk_signal_new ("pressed",                    GTK_RUN_FIRST,                    object_class-&gt;type,                    GTK_SIGNAL_OFFSET (GtkButtonClass, pressed),                    gtk_marshal_NONE__NONE,                    GTK_TYPE_NONE, 0);  button_signals[RELEASED] =    gtk_signal_new ("released",                    GTK_RUN_FIRST,                    object_class-&gt;type,                    GTK_SIGNAL_OFFSET (GtkButtonClass, released),                    gtk_marshal_NONE__NONE,                    GTK_TYPE_NONE, 0);  button_signals[CLICKED] =    gtk_signal_new ("clicked",                    GTK_RUN_FIRST | GTK_RUN_ACTION,                    object_class-&gt;type,                    GTK_SIGNAL_OFFSET (GtkButtonClass, clicked),                    gtk_marshal_NONE__NONE,                    GTK_TYPE_NONE, 0);  button_signals[ENTER] =    gtk_signal_new ("enter",                    GTK_RUN_FIRST,                    object_class-&gt;type,                    GTK_SIGNAL_OFFSET (GtkButtonClass, enter),                    gtk_marshal_NONE__NONE,                    GTK_TYPE_NONE, 0);  button_signals[LEAVE] =    gtk_signal_new ("leave",                    GTK_RUN_FIRST,                    object_class-&gt;type,                    GTK_SIGNAL_OFFSET (GtkButtonClass, leave),                    gtk_marshal_NONE__NONE,                    GTK_TYPE_NONE, 0);  gtk_object_class_add_signals (object_class, button_signals, LAST_SIGNAL);  object_class-&gt;set_arg = gtk_button_set_arg;  object_class-&gt;get_arg = gtk_button_get_arg;  widget_class-&gt;activate_signal = button_signals[CLICKED];  widget_class-&gt;realize = gtk_button_realize;  widget_class-&gt;draw = gtk_button_draw;  widget_class-&gt;draw_focus = gtk_button_draw_focus;  widget_class-&gt;draw_default = gtk_button_draw_default;  widget_class-&gt;size_request = gtk_button_size_request;  widget_class-&gt;size_allocate = gtk_button_size_allocate;  widget_class-&gt;expose_event = gtk_button_expose;  widget_class-&gt;button_press_event = gtk_button_button_press;  widget_class-&gt;button_release_event = gtk_button_button_release;  widget_class-&gt;enter_notify_event = gtk_button_enter_notify;  widget_class-&gt;leave_notify_event = gtk_button_leave_notify;  widget_class-&gt;focus_in_event = gtk_button_focus_in;  widget_class-&gt;focus_out_event = gtk_button_focus_out;  container_class-&gt;add = gtk_button_add;  container_class-&gt;remove = gtk_button_remove;  container_class-&gt;child_type = gtk_button_child_type;  klass-&gt;pressed = gtk_real_button_pressed;  klass-&gt;released = gtk_real_button_released;  klass-&gt;clicked = NULL;  klass-&gt;enter = gtk_real_button_enter;  klass-&gt;leave = gtk_real_button_leave;}&#13;</pre>          </td>        </tr>      </table>    </div>    <div class="NAVFOOTER">      <br>      <br>      <table width="100%" border="0" bgcolor="#ffffff" cellpadding=       "1" cellspacing="0">        <tr>          <td width="25%" bgcolor="#ffffff" align="left">            <a href="z105.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-gtkarg.html"><font color="#0000ff" size=            "2"><b>Next &gt;&gt;&gt;</b></font></a>          </td>        </tr>        <tr>          <td colspan="2" align="left">            <font color="#000000" size="2"><b>Type Checking and New            Types</b></font>          </td>          <td colspan="2" align="right">            <font color="#000000" size="2"><b><span class=             "STRUCTNAME">GtkArg</span> and the Type            System</b></font>          </td>        </tr>      </table>    </div>  </body></html>

⌨️ 快捷键说明

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