📄 sec-classinit.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> <<< 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 >>></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>. </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. </p> </li> <li> <p> There is no base class initializer for <tt class= "CLASSNAME">GtkWidget</tt>, or it would be called. </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. </p> </li> <li> <p> There is no base class initializer for <tt class= "CLASSNAME">GtkBin</tt>, or it would be called. </p> </li> <li> <p> There is no base class initializer for <tt class= "CLASSNAME">GtkButton</tt>, or it would be called. </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. </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"> GtkObject |GtkWidget | GtkContainer | GtkBin | GtkButton </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"> 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->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->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->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->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->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->set_arg = gtk_button_set_arg; object_class->get_arg = gtk_button_get_arg; widget_class->activate_signal = button_signals[CLICKED]; widget_class->realize = gtk_button_realize; widget_class->draw = gtk_button_draw; widget_class->draw_focus = gtk_button_draw_focus; widget_class->draw_default = gtk_button_draw_default; widget_class->size_request = gtk_button_size_request; widget_class->size_allocate = gtk_button_size_allocate; widget_class->expose_event = gtk_button_expose; widget_class->button_press_event = gtk_button_button_press; widget_class->button_release_event = gtk_button_button_release; widget_class->enter_notify_event = gtk_button_enter_notify; widget_class->leave_notify_event = gtk_button_leave_notify; widget_class->focus_in_event = gtk_button_focus_in; widget_class->focus_out_event = gtk_button_focus_out; container_class->add = gtk_button_add; container_class->remove = gtk_button_remove; container_class->child_type = gtk_button_child_type; klass->pressed = gtk_real_button_pressed; klass->released = gtk_real_button_released; klass->clicked = NULL; klass->enter = gtk_real_button_enter; klass->leave = gtk_real_button_leave;} </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> <<< 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 >>></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 + -