[Gtk-sharp-list] Gtk# constructors

Charles Iliya Krempeaux charles@reptile.ca
18 Feb 2003 09:51:52 -0800


On Tue, 2003-02-18 at 08:59, Lee Mallabone wrote:
> On Tue, 2003-02-18 at 16:43, Charles Iliya Krempeaux wrote:
> > [...]
> > 
> > > 2: public HBox(IntPtr raw) : base(raw) {}
> > 
> > This one is necessary for people creating bindings.  You would
> > use this if you had a PInvoke procedure that returned a (C based)
> > GtkHBox struct.
> Maybe I've got the wrong end of the stick here... Are you talking about people making bindings around the C# API, but using the C API themselves to manipulate their C based Gtk+ objects with the Gtk C# calls?
> Because if that's what you're saying, Why would anyone do that?? :)

No.  (I'm talking about people who are wrapping C API's.)

Let me give you an example.  Maybe that would be helpful.

OK, we have the C API called librsvg.  (Also just called rsvg.)
Now, I am making a .NET wrapper to this.  That way people can
use librsvg in the C# programs.

Now, alot of the work is automatically done [basically] by two
programs -- gapi.pl and gapi_codegen.exe.  However, not everything
is done by these two programs.  So I had to do some wrapping by

In the librsvg C API, there is the following procedure which did
NOT get wrapped:

    GdkPixbuf *rsvg_pixbuf_from_file( const gchar  *file_name
                                    , GError **error

So, I had to wrap this myself, by hand.  So... I imported that
C procedure into C# using the following call:

  static extern
  System.IntPtr rsvg_pixbuf_from_file( string file_name
                                     , out System.IntPtr error

(Don't worry about what it means, or why it looks different than the
C declaration.  It's just how you import that C procedure into C#.)

Then, I make a C# procedure with the following signature:

    public static Gdk.Pixbuf PixbufFromFile(string file_name)

(This is what people writing C# programs will call.)

Now, this procedure returns a C# "Gdk.Pixbuf" class.  But (the
C procedure) "rsvg_pixbuf_from_file" returns a "GdkPixbuf *".

So I need to change a the C struct "GdkPixbuf *" into the
C# class "Gdk.Pixbuf".

And the way I do that is through the constructor:

    Gdk.Pixbuf(IntPtr raw)

This constructor lets me pass it a C struct "GdkPixbuf *" and
gives me a C# class "Gdk.Pixbuf".

Basically, the call comes down to a:

  public static Gdk.Pixbuf PixbufFromFile(string file_name)
      return new Gdk.Pixbuf( rsvg_pixbuf_from_file(file_name, error) );

(Although the real code does more stuff... like error checking.
But....)  Notice that the C procedure is being called; and the
C struct "GdkPixbuf *" is being passed to that constructor, which
returns a C# class "Gdk.Pixbuf".  Which is what we wanted.

The same goes for that HBox constructor you mentioned.

I hope that explained things.

See ya

     Charles Iliya Krempeaux, BSc

 Reptile Consulting & Services    604-REPTILE    http://www.reptile.ca/