Документ взят из кэша поисковой машины. Адрес
оригинального документа
: http://www.sao.ru/cats/~satr/GGAD/sec-classinit.html
Дата изменения: Wed Jul 12 23:54:26 2000 Дата индексирования: Tue Oct 2 02:15:48 2012 Кодировка: Поисковые слова: earth's atmosphere |
GTK+ / Gnome Application Development | |||
---|---|---|---|
<<< Previous | Home | Next >>> |
When a type is first used, GTK+ creates an instance of its class struct (using the information supplied to gtk_type_unique()). 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.
Next, the base class initialization functions of each parent class and that of the class itself are called in order, starting with GtkObject. (The base class init function is the last argument to gtk_type_unique()). A base class initializer is optional; in the GtkButton 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.
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).
A concrete example should make the class creation process clear. The class hierarchy for GtkButton is shown in Figure 1. When the GtkButton type is registered, an empty GtkButtonClass is created. This class struct is initialized as follows:
The class struct for GtkBin, GtkButton's immediate parent, is copied into it. This means GtkButton inherits class functions from GtkBin.
The base class initialization function for GtkObject is called on it. This zeroes some GtkObject class functions that should not be inherited.
There is no base class initializer for GtkWidget, or it would be called.
The base class initializer for GtkContainer is called. This zeroes some GtkContainer class functions that should not be inherited, and initializes a GtkContainerClass data member.
There is no base class initializer for GtkBin, or it would be called.
There is no base class initializer for GtkButton, or it would be called.
The class initializer is called for GtkButton. This fills in the GtkButtonClass structure, registers signals, and registers object arguments.
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.
GtkObject | GtkWidget | GtkContainer | GtkBin | GtkButton |
Here is the GtkButton class initialization function, just to give you an initial sense of things; read on to learn what this code does.
static void gtk_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; } |