[Gtk-sharp-list] Gtk.Builder: now usable in svn

Stephane Delcroix stephane at delcroix.org
Tue Oct 28 07:49:51 EDT 2008


*** This message document some API changes I pushed in SVN, please
discuss before it gets released ***

As of r117257, Gtk.Builder (the glade replacement) is now usable in
gtk-sharp.

I had to make some API changes, but I guess no-one was using Builder yet
in gtk-sharp. Here's a list of changes:

* Added ctors to mimic Glade.XML API:
	Builder (System.IO.Stream stream);
	Builder (System.IO.Stream stream, string translation_domain);
	Builder (string resource_name);
	Builder (string resource_name, string translation_domain);
	Builder (Assembly assembly, string resource_name, string
translation_domain);

* Inner classes Builder.HandlerNotfoundException and
Builder.ObjectAttribute

* public void Autoconnect (object handler)
* public void Autoconnect (Type handler_type)
* public IntPtr GetRawObject (string name), see below.
* hiding ConnectSignals

Porting a glade# application to builder is as straightforward as
replacing "Glade.XML" by "Gtk.Builder" and the "[Glade.Widget]"
attribute markers by "[Builder.Object]" as shown in the example below,
pretty close to the Glade exemple in monodoc:
http://www.go-mono.com/docs/index.aspx?tlink=4@ecma%3a372%23XML%2f


====Example1
using System.Runtime.InteropServices;
using System;
using Gtk;

public class MyWindow
{
        [Builder.Object] Label label1;
        [Builder.Object] Window window1;
        
        static void Main (string [] args)
        {
                new MyWindow (args);
        }

        public MyWindow (string [] args)
        {
                Application.Init ();

                Builder builder = new Builder ("builder.ui");
                builder.Autoconnect (this);
                window1.ShowAll ();
                Application.Run ();
        }

        public void HandleButton1Clicked (object o, EventArgs e)
        {
                Console.WriteLine ("Clicked");
                label1.Text = "Clicked";
        }
}

But things start to become very interesting when you use the
GetRawObject addition, allowing you to create component using builder
and still inherit from the right Gtk object. In the previous example,
and as it was done with glade#, MyWindow does not inherit from
Gtk.Window. The following example shows how to create a Window (or any
other gtk widget) from builder and still inherits from Gtk.Window:

====Example2
using System.Runtime.InteropServices;
using System;
using Gtk;

public class BuilderTest
{
        static void Main ()
        {
                Application.Init ();

                Window w1 = new MyWindow ();
                w1.ShowAll ();
                Application.Run ();
        }

        public class MyWindow : Gtk.Window
        {
                [Builder.Object] Label label1;

                public MyWindow () : this (new Builder ("builder.ui"),
"window1")
                {
                        label1.Text = "It's even simpler this way";
                }

                private MyWindow (Builder builder, string window_name)
                        : base (builder.GetRawObject (window_name))
                {
                        builder.Autoconnect (this);
                }

                public void HandleButton1Clicked (object o, EventArgs e)
                {
                        Console.WriteLine ("Clicked");
                }
        }
}

I find this construction pretty neat and useful (See attached
builder1.cs file for a way to hide those stuffs in an abstract class).

I attached by builder.ui file as weel, compile the example using gmcs
-pkg:gtk-sharp-2.0 -resource:builder.ui example.cs

regards

Stephane


-------------- next part --------------
A non-text attachment was scrubbed...
Name: builder.ui
Type: application/x-designer
Size: 1478 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/gtk-sharp-list/attachments/20081028/d084b531/attachment.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: builder1.cs
Type: text/x-csharp
Size: 1003 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/gtk-sharp-list/attachments/20081028/d084b531/attachment-0001.bin 


More information about the Gtk-sharp-list mailing list