[Gtk-sharp-list] Glade# and resources.

Ricardo Fernández Pascual rfp1@ono.com
13 Sep 2002 17:17:54 +0200


--=-SLvHef7Cs/uS4SSnD7ZJ
Content-Type: text/plain; charset=ISO-8859-15
Content-Transfer-Encoding: quoted-printable

El jue, 12-09-2002 a las 20:01, Miguel de Icaza escribi=F3:
> Hey guys,
>=20
>     In the .NET universe you can "embed" into your executable resources
> files, like for instance the .glade files, could we add a constructor
> that would allow a developer to load the glade file from its resource?

Looking closer at this, there's no need to modify the library to do
this. It's really easy already.

To simplify it even further. I have done the following:
    - Added a constructor to the Glade.XML class that takes a Stream
    instead of a file name.
    - Modified the sample program to embed the glade file in the
    executable.

To make it even simpler, I could add another constructor that takes an
Assembly object and a resource name. Is this a good idea?

With that, to create a Glade.XML object you could do:

Glade.XML gxml =3D new Glade.XML (Assembly.GetExecutingAssembly (), "file.g=
lade",
                                "main_widget_name", null);

May I commit?

--=20
Ricardo Fern=E1ndez Pascual
ric@users.sourceforge.net
Murcia. Espa=F1a.

--=-SLvHef7Cs/uS4SSnD7ZJ
Content-Description: 
Content-Disposition: inline; filename=resource.diff
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-15

Index: glade/XML.custom
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvs/public/gtk-sharp/glade/XML.custom,v
retrieving revision 1.2
diff -u -r1.2 XML.custom
--- glade/XML.custom	13 Sep 2002 11:38:36 -0000	1.2
+++ glade/XML.custom	13 Sep 2002 15:08:22 -0000
@@ -45,6 +45,22 @@
 			return ret;
 		}
=20
+		/* a constructor that reads the XML from a Stream */
+
+		[DllImport("glade-2.0")]
+		static extern IntPtr glade_xml_new_from_buffer(byte[] buffer, int size, =
string root, string domain);
+
+		/// <summary>Creates a Glade.XML object from a Stream</sumary>
+		/// <remarks>Reads the contents of the stream and parses it. It must be =
in=20
+		/// correct Glade format</remarks>
+		public XML (System.IO.Stream s, string root, string domain)
+		{
+			int size =3D (int) s.Length;
+			byte[] buffer =3D new byte[size];
+			s.Read (buffer, 0, size);
+			Raw =3D glade_xml_new_from_buffer(buffer, size, root, domain);
+		}
+
 		/* signal autoconnection using reflection */
=20
 		/// <summary>Automatically connect signals</summary>
Index: sample/GladeTest.cs
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvs/public/gtk-sharp/sample/GladeTest.cs,v
retrieving revision 1.1
diff -u -r1.1 GladeTest.cs
--- sample/GladeTest.cs	13 Sep 2002 11:38:36 -0000	1.1
+++ sample/GladeTest.cs	13 Sep 2002 15:08:22 -0000
@@ -11,6 +11,8 @@
 	using Gnome;
 	using Glade;
 	using GtkSharp;
+	using System.IO;
+	using System.Reflection;
=20
 	public class GladeTest : Program
 	{
@@ -21,8 +23,16 @@
 		public GladeTest (string[] args, params object[] props)=20
 			: base ("GladeTest", "0.1", Modules.UI, args, props)
 		{
-			Glade.XML gxml =3D new Glade.XML ("test.glade", "main_window", null);
+			/* Note that we load the XML info from the assembly instead of using=20
+			   an external file. You don't have to distribute the .glade file if=20
+			   you don't want */
+			Assembly a =3D Assembly.GetExecutingAssembly ();
+			Stream s =3D a.GetManifestResourceStream ("test.glade");
+
+			Glade.XML gxml =3D new Glade.XML (s, "main_window", null);
 			gxml.Autoconnect (this);
+		=09
+			s.Close ();
 		}
=20
 		public void OnWindowDeleteEvent (object o, DeleteEventArgs args)=20
Index: sample/Makefile.in
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /cvs/public/gtk-sharp/sample/Makefile.in,v
retrieving revision 1.17
diff -u -r1.17 Makefile.in
--- sample/Makefile.in	13 Sep 2002 11:38:36 -0000	1.17
+++ sample/Makefile.in	13 Sep 2002 15:08:22 -0000
@@ -50,8 +50,8 @@
 glade-viewer.exe: GladeViewer.cs
 	$(MCS) --unsafe -o glade-viewer.exe $(local_paths) $(all_assemblies) Glad=
eViewer.cs
=20
-glade-test.exe: GladeTest.cs
-	$(MCS) --unsafe -o glade-test.exe $(local_paths) $(all_assemblies) GladeT=
est.cs
+glade-test.exe: GladeTest.cs test.glade
+	$(MCS) --unsafe -resource:test.glade -o glade-test.exe $(local_paths) $(a=
ll_assemblies) GladeTest.cs
=20
 clean:
 	rm -f *.exe

--=-SLvHef7Cs/uS4SSnD7ZJ--