Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#
Article

Glade Code Changer

Rate me:
Please Sign up or sign in to vote.
4.11/5 (3 votes)
18 Jul 20074 min read 30.7K   467   14   2
This article introduces a code generator for Glade that produces C# code

Screenshot - GSwindows.jpg

Introduction

This program is a code generator for Glade that produces C# code. It will make it nearly as easy for you to develop a cross-platform GUI with Gtk as it is with Windows Forms. The generator has been tested on both Windows and Linux with Mono.

Prerequisites

To be able to run Gtk# GUIs on Windows, you need the Gtk# runtime. You can get it here; it is 23.3 Mb. The GUI designer Glade can be downloaded here. Alternatively, if you have Mono for Windows installed, it is under Start-> Mono 1.2.4 for Windows -> Applications -> Glade. If you are using Linux and are interested in this article, I presume that you already have Mono installed and that you are more familiar with this topic. Thus, I will explain in more detail for Windows users.

Cross-platform GUIs: left Linux and right Windows

How does it function? The tool is pretty simple. It watches the *.glade file that you selected in the upper file chooser for changes. A change in this case means that you saved it in Glade. So, when it detects the change it parses the Glade file for widgets and signals. Then it puts them into your code as specified in the *.cs file in the second file chooser. I will later go into more detail.

Quickstart

I am now going to explain the procedure for VS 2005. After you have downloaded and installed the Gtk# runtime, download GladeSharperTemplate and put the ZIP file like it is (unzipped!) in the directory: My files\Visual Studio 2005\Templates\Project Templates. Start VS and begin a new GladSharperProject from the templates. The main file looks like this:

C#
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingGtk;
usingGlade;

namespaceGladeSharperProject1
{
    publicclass GladeApp
    {
        staticvoid Main(string[] args)
        {
            newGladeApp(args);
        }

        publicGladeApp(string[] args)
        {
            Application.Init();
            stringstandardNamespace = 
                System.Reflection.Assembly.GetExecutingAssembly(
                ).GetName().Name;
            Glade.XMLgxml = 
                new Glade.XML(null, 
                standardNamespace + "." + "gui.glade", null, null);
            gxml.Autoconnect(this);
            //window1.Show();
            Application.Run();
        }
    }
}

Now download and start GladeSharper. As a Glade file, you select gui.glade in your Project folder and as a C# file you select GladeAppDummy.cs -- i.e. the file from above -- in the same folder. Click "Start watcher." Now you can start Glade and open gui.glade in your Project folder again. Design your GUI and save it. Then the widgets and signals are made available in your Editor. You just have to reload the code when VS asks you. Don't forget to uncomment window1.Show() after you've created the first window.

To make the application react on the Close button, you create the signal destroy for your window. Save the Glade file. The signal will be added to your code. Then put in Application.Quit(), as shown below:

C#
voidon_window1_destroy(object sender, EventArgse)
{
    Application.Quit();
}

Details

The widgets are placed in #region GtkWidgetDeclarations. Don't write any code within this section, as it will be deleted. However, you can move the whole region and it will stay there when the code is updated. You can move the signal handlers freely in your code wherever you like; they will not be moved again or deleted. They also won't get deleted when you deactivate the signal in Glade! So, if a signal is no longer responding, have a look at Glade's properties!

To find out which EventArgs a signal uses, I had to use Reflection. In the Glade file is the name of the widget and the signal. With Reflection, I look up the signal and find out which EventArgs it uses. You will have something like this:

C#
voidon_window1_client_event(object sender, Gtk.ClientEventArgse)
{
}

voidon_togglebutton1_toggled(object sender, System.EventArgse)
{
}

As you can see, the EventArgs have full qualifiers like System.EventArgs. Sometimes the name of the signal cannot be found in the Gtk# API. In this case, the signal will also be put in the code and it will be functional. It could be, however, that it has special EventArgs. You will recognize this through seeing that EventArgs is not qualified, i.e. there is no preceeding System.

C#
voidon_button1_activate(object sender, EventArgse)
{
}

That is the one example I spoke of. The Event here is called Activate, but in the Gtk# API it is called Activated. Either way, the EventArgs of Button.Activated are System.EventArgs so it makes no diference. When you see a signal handler with unqualified EventArgs, you know that GladeSharper was not able to find it in the API. If you look by hand, it may be that the signal has more special EventArgs.

Conclusion

A last hint: If you are unsure about whether you saved your code, when the Editor tells you that the code has changed and asks if you want to reload it, just click No. Save your code and click Save in Glade again. I hope you like the generator! If you are missing functions, post in detail in the comments section!

History

  • 18 July, 2007 -- Original version posted

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralLittle bug but great features! Pin
Member 167849518-Aug-08 3:30
Member 167849518-Aug-08 3:30 
GeneralVery interesting Pin
gerektoolhy18-Jul-07 4:26
gerektoolhy18-Jul-07 4:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.