Re: How to "extend" a widget?



On 12/16/2011 02:52 PM, jjacky wrote:
Thanks. I will have a look into Vala, although for projects I have 
planned, I really want/need to be using C.

Precisely.  Vala makes the C Gobjects.  You could use Vala to construct
a class with a bunch of empty methods and then use the generated C code
and fill in your code, abandoning the vala as a scaffold.

And while GTK is oriented-object, it is written in C and, AFAIK, there 
are no such things as classes in C?

"Classes" and other Object-oriented things are merely syntactic sugar in
any language.  GTK is certainly object-oriented through and through with
all the classic aspects of Polymorphism, inheritance, and encapsulation.

I believe the way to create a widget based on/extending another one is 
how I have done so far (based on the docs /tutorials I read), only I'm 
not sure how to properly do things like "overriding" functions to change 
behaviors or add features.

Overriding methods is done through the GOBject's vtable.  Virtual
methods (as in C++) are dispatched through a function table.  In the raw
C code of GTK objects, this is a special struct.  At least the way it
used to work in GTK (my knowledge is a bit rusty) was that one struct
contained the object's data, and this other stuct (sometimes called a
'klass' struct), contained the vtable.  Any call to a virtual method
would, using the object struct pointer itself, look up the klass
structure, and look up the method to call (classic polymorphism).


If Vala allows to do that easily and does produce C code though, I might 
try and see how it does/what code it produces, but I'm not sure learning 
a new language is something I wanna get into right now...

Sure.  as I said in my e-mail GTK developers used to use a tool called
gob to create GObject-based classes, but that also has a
pseudo-language.  That said, working with GOBjects in C requires a lot
of boilerplate code (to implement the object-orientedness), so using
Vala or GOB (whatever the latest incarnation is), seems like a good idea
to me, specially if you want to make your own classes and inherit, etc.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]