On 23/11/2015 11:48, Hendrik Boom wrote:
> On Sun, Nov 22, 2015 at 01:06:04PM +0100, Edward Bartolo wrote:
>> Hi All,
>>
>> Is it possible to use classes and objects while using gtk2/3? I
>> noticed that only functions are used and that class use is ovoided by
>> prepending functions with a group string. As far as I know, C can
>> still use structs that are attached to a set of data and a set of
>> functions, but without inheritance and polymorphyism.
>>
>> Edward
>
> I believe gtk has its own run-time system of classes and inheritance
> built into it in its C version. The C++ version is called gtk+, and
> pastes C++ inheritance on top of this.
>
> (not sure of the details, but it's worth looking up the details in case
> I get some of it wrong)
It's Gtkmm:
https://developer.gnome.org/gtkmm/2.24/classGtk_1_1Widget.html
The C "interitance" is awful to use and awful to create your own classes
with. In all seriousness, you should never ever use it. The GTK
developers themselves invented a whole new language ("Vala") to
preprocess into C simply to avoid its horrors. It only took 15 years
for them to realise that you can't robustly implement OO in plain C, but
you can /generate/ it. Doing it by hand is a maintenance nightmare.
Note, this isn't an endorsement of Vala either, which has its own
(different) set of problems, particualarly when it comes to debugging
(since debugging generated code is always painful, and even more so if
the generator itself has bugs).
Gtkmm is a thin wrapper around the C "classes", but it makes them into
first-class C++ classes, along with all the properties, signals etc. If
you want to create your own classes, this is the way to do it. "class
MyWindow : public Gtk::Window". Job done. Override the parent's
virtual methods, add your own properties and signals, it's simple and
just works. And it's debuggable. You can break in the debugger and get
a meaningful stacktrace showing exactly where you are in your own code,
through the C++ wrappers to the C functions (and back again via signals).
After using plain GTK+ and several of its bindings (Perl, Python, C++),
I'd say GTKmm is the best binding in terms of its quality of
implementation and ease of use; the Python bindings are a close second.
But if you're using C++, I'd suggest you look at Qt5 which is vastly
better than GTK+, especially with the advent of GTK+3. It's even nicer
to use, and doesn't have the added baggage of being a wrapper.
Regards,
Roger