Re: [Vala] How to chain up to parent constructor



On Fri, Jan 01, 2010 at 12:02:31 +0100, Jiří Zárevúcky wrote:
Dov Grobgeld píše v Pá 01. 01. 2010 v 12:13 +0200:
How do you create a "transparent" inheritance? E.g. I would like to inherit
from Gtk.RadioToolButton and support all its methods and constructors.

But when creating the proxy class FooRadioToolButton below I get the error
"The name `from_stock' does not exist in the context of
`FooRadioToolButton'" though from_stock exists in Gtk.RadioToolButton. What
did I do wrong?


Constructors are never automatically inherited (it wouldn't make much
sense in most cases, don't you think?). You need to chain it up
yourself.

Example:

public class FooRadioToolButton : Gtk.RadioToolButton {
      public FooRadioToolButton.from_stock (SList<Gtk.Widget> list,
StockWhatever stock) {
              base.from_stock (list, stock);  
      }
}

Unfortunately this isn't going to work, because
Gtk.RadioToolButton.from_stock, like all Gtk allocators (*_new functions),
lacks class-method form (*_construct function), which is necessary for the
chain-up.

However, for all Gtk objects it is allowed to chain up directly to
GLib.Object, passing parameters as construct properties. So you need to chain
up like (I am not really sure the syntax is correct, though):

public class FooRadioToolButton : Gtk.RadioToolButton {
        public FooRadioToolButton.from_stock (SList<Gtk.Widget> list,
                                                string stock) {
                Object ("group", list, "stock-id", stock);
        }
}

(note, that the stock id is a string, as that's what the original method
wants).

The reason behind all this is, that the gtk_radio_tool_button_new_from_stock
allocates and initializes a Gtk.RadioToolButton, but we need to allocate
a subclass and we can't tell it. Vala normally generates for each _new
function a _construct function, that has additional parameter, the GLib.Type
to allocate, but Gtk does not have them. The only exception is g_object_new.
Gtk is written so that all it's objects can be constructed with g_object_new.

-- 
                                                 Jan 'Bulb' Hudec <bulb ucw cz>



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