Re: [Vala] Access the "klass" parameter of a class_init() function in vala with "static construct"



2010/8/1 Evan Nemerson <evan coeus-group com>

On Sat, 2010-07-31 at 22:08 +0200, Sébastien Wilmet wrote:


2010/7/31 Evan Nemerson <evan coeus-group com>
        On Sat, 2010-07-31 at 17:59 +0200, Sébastien Wilmet wrote:
        > Hello!
        >
        > What I want to do is to implement a GtkAction derived class
        which sets
        > GtkActionClass:toolbar_item_type to
        GTK_TYPE_MENU_TOOL_BUTTON in its
        > class_init function.


        Vala doesn't currently expose *Class structs in the bindings,
        so AFAIK
        what you're talking about can't be done easily. I'm not
        exactly an
        expert on GObject, but I think you should be able to hack
        something
        together by going creating bindings for GtkActionClass then
        getting
        access to it with GLib.Object.get_class. This is untested, but
        you
        should get the idea:


        /* In a vapi */
        [Compact, CCode (cname = "GtkActionClass")]
        public class GtkActionClass {
         public GLib.Type menu_item_type;
         public GLib.Type toolbar_item_type;
        }

        /* Your code */
        unowned GtkActionClass ac = (GtkActionClass) this.get_class
        ();
        ac.toolbar_item_type = typeof (MenuToolButton);


I have this error:
error: Access to instance member `GLib.Object.get_class' denied

You aren't going to be able to access it from a static construct block.
Try it from within a regular construct block.


Yeah it works! Thanks. I thought it was an obligation to put the code in the
static construct block since in C it is in *class_init().

So here is my code (with the vapi you wrote):

using Gtk;

public class MenuToolAction : Action
{
    public MenuToolAction (string name, string? label, string? tooltip,
string? stock_id)
    {
        GLib.Object (name: name, label: label, tooltip: tooltip, stock_id:
stock_id);
        unowned GtkActionClass ac = (GtkActionClass) get_class ();
        ac.toolbar_item_type = typeof (MenuToolButton);
    }
}


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