Without going around so much I hope you can help me with something that has me stranded for a whole week, I have a serious confusion with GTK+ and its object system, reading the GNOME project documentation I understood that it uses GType to have a list of all objects and their properties and GObject
it is an abstract class with all the required functions basically.
Now my confusion is in the two functions _init
and class_init
, one initializes the instance of the class or the object and the other the class as such, inside class_init
you have to initialize all the properties, virtual functions etc, but I found the mechanism for the object properties, explained here and I got very lost. Why aren't the properties included in the structure of the object? Searching a bit I found this answer in this community which half explained it to me but left the other half still in doubt, I show you my two structures:
object structure
struct _WindowPrincipal
{
GtkWindow *padre;
GtkMenuBar *menuBar;
GtkDrawingArea *areaDibujo;
GtkGrid *grid;
};
class structure
struct _WindowPrincipalClass
{
GtkWindowClass *clasePadre;
GtkMenuBar (*menu) ();
void (*design) (WindowPrincipal *self);
};
As you can see, they don't have anything special, in _WindowPrincipal
I define the parent that is a GtkWindow
and the properties of that instance, it has a menu, a drawing area and a grid for the window design, in the structure of the class two functions for menu design and window design.
In the function class_init
, the properties are registered with GParamType
, but I don't find logic in this, that is, why do I define the properties in the structure of the object if class_init
I am going to register them to the class and the object respectively? what logic does that have? Or am I confusing myself with two things that have nothing to do with each other? I honestly can't understand it.
C does not natively admit inheritance or polymorphism because it is not object oriented... however, there are mechanisms that allow this behavior to be simulated... but you have to program them by hand.
Thus, a series of utilities must be manually implemented:
dynamic_cast
in C++)Obviously it is not necessary to implement all these features... but depending on the design you propose, it may be necessary to implement more than one.
How does GTK work?
C does not natively support a multitude of C++ features, so if you want to emulate any of these features you have to program it by hand.
In C++:
Namely:
However, in GTK they have chosen to isolate these two concepts for, among other reasons, isolation, reuse:
So, in GTK, you have a structure to handle function pointers and another to handle member variables:
These two classes end up being linked together in the GTK core with the subsequent configuration that needs to be done. Notice that up to now the only contact that these structures have had with GTK is limited to including GTK's own structures and pointers... at no time have we told GTK that it should treat a new type, let alone how it should treat it.
Your initial approach is good but you still haven't configured the GTK environment to know how to work with your type.