It’s bloody useless and really simple, but it works and was really easy to write. I created the GUI in Glade, a GNOME program which outputs the UI as XML. Then using Glade# I can load the UI and connect the signals from the XML file. And since in C# I can include resource files in my binary at compile time, I don’t even have to distribute the .glade XML file, I can compile it in as a resource.
Why is all this cool? Well, for me, writing GNOME applications in C has been quite challenging and has a steep learning curve. C# is a pretty easy language to program in and when you throw in Glade# you have a winner. This is also neat because the compiled binary will run in Windows provided the system has Gtk# and Glade# installed.
All the app does is produce a small window with a button that says “Press me!” When you click the button it writes to the console text saying that it was pressed. So I won’t bother including a screenshot. But click below for the really simple code.
// CSharp.cs
//
// Author: Jamin P. Gray
//
// (c) 2002 Jamin P. Gray
namespace GladeSamples {
using System;
using Gtk;
using Gnome;
using Glade;
using GtkSharp;
using System.IO;
using System.Reflection;
public class CSharp
{
public static void Main (string[] args)
{
new CSharp(args);
}
public CSharp (string[] args)
{
Application.Init();
Glade.XML gxml = new Glade.XML (null, "c-sharp.glade", "window", null);
gxml.Autoconnect (this);
Application.Run();
}
public void OnWindowDeleteEvent (object o, DeleteEventArgs args)
{
Application.Quit();
args.RetVal = true;
}
public void OnButtonClicked (System.Object obj, EventArgs e)
{
Console.WriteLine ("Button 1 clicked");
}
}
}
and the XML file looks like this:
<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">
<glade-interface>
<widget class="GtkWindow" id="window">
<property name="visible">True</property>
<property name="title" translatable="yes">Gtk#</property>
<property name="type">GTK_WINDOW_TOPLEVEL</property>
<property name="window_position">GTK_WIN_POS_NONE</property>
<property name="modal">False</property>
<property name="default_width">100</property>
<property name="default_height">100</property>
<property name="resizable">True</property>
<property name="destroy_with_parent">False</property>
<signal name="delete_event" handler="OnWindowDeleteEvent" last_modification_time="Thu, 12 Dec 2002 17:30:05 GMT"/>
<child>
<widget class="GtkButton" id="button">
<property name="visible">True</property>
<property name="tooltip" translatable="yes">Press the button</property>
<property name="can_focus">True</property>
<property name="label" translatable="yes">Press Me!</property>
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_HALF</property>
<signal name="clicked" handler="OnButtonClicked" last_modification_time="Thu, 12 Dec 2002 17:28:36 GMT"/>
</widget>
</child>
</widget>
</glade-interface>
Tags: GNOME










December 12th, 2002 at 12:58 pm
To compile (given where my libraries are installed):
mcs /unsafe /resource:c-sharp.glade -L /usr/local/lib -r gtk-sharp.dll -r glade-sharp.dll CSharp.cs
September 13th, 2004 at 6:28 pm
And how to run the program. mono xxx.exe ?
January 25th, 2005 at 4:03 pm
I wish to know more about glade#, I have mono and glade but how does one use glade w/ C#?
Please point me to the right direction, where do I get glade#?
Thanks for your help
Lito Cruz
May 26th, 2005 at 4:02 pm
To the best of my understanding, you don’t need Glade# — you just use normal Glade. I’m guessing Glade# is a misnomer. Glade just outputs an XML file, and you can use that XML file anywhere.
May 26th, 2005 at 5:12 pm
Glade# is not a misnomer; it’s the mono bindings for libglade which are necessary to load the UI from the XML file.
May 31st, 2005 at 12:47 pm
If I make a c# application, and wish to distribute it to employees/customers that are running various versions of windows, what do I need to package with my installer in order for them to be able to run my program?
Thanks, Matthew
May 31st, 2005 at 12:53 pm
Well that all depends. At the very least your users will need a .NET runtime…either Microsoft’s or Mono. If you use the Gtk# toolkit you’ll also need to install Gtk-sharp. There are installers available on the Mono Project Download page
November 28th, 2005 at 8:03 pm
Thanks Jamin,
You have saved me hours of time trying to work out how it is done.
Nice bit of code.
Kyle.
May 15th, 2006 at 1:31 pm
So, after I create the “c-sharp.glade” XML file using Glade, how do I get the empty C# functions into the “CSharp.cs” file?
I’m hoping that I don’t have to remember the prototypes for every different sort of event handler…
May 15th, 2006 at 1:46 pm
You have to create the event handlers yourself. How would a tool or IDE magically know which event handlers you need for your particular application? You don’t have to mentally remember all of the event handlers, though. That’s what documentation is for. Have you tried monodoc or monodevelop?
May 15th, 2006 at 4:04 pm
I noticed that the XML file only mentions two event handlers. I haven’t used Glade yet, but presumably you somehow gave Glade the names that these event handlers would have. You told it which events you need and it knows exactly which classes they are.
Shouldn’t it then be fairly straightforward for a tool to do the lookup for you? Button click handlers go this way, form delete handlers go that way… Computers excell at things like this.
Doesn’t Glade autogenerate code at all? Maybe I’m thinking of KDevelop, it’s been a long while, but this seems pretty fundamental for a graphical development tool.