SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

Matt Ward

Creating a Gtk# App with SharpDevelop 2.0 Using Glade

The starting point is the Glade xml file.

Glade xml file

This was created using the Glade UI Builder that ships with the Mono installer. The xml file specifies the UI components and signals (events) for these components. In our case the main components we have are a mainWindow and a messageLabel. There is also a delete_event signal defined for the main window named MainWindowDeleteEvent.

<?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="mainWindow">
  <property name="visible">True</property>
  <property name="title" translatable="yes">Hello, Gtk# World</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">300</property>
  <property name="default_height">300</property>
  <property name="resizable">True</property>
  <property name="destroy_with_parent">False</property>
  <property name="decorated">True</property>
  <property name="skip_taskbar_hint">False</property>
  <property name="skip_pager_hint">False</property>
  <property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
  <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
  <signal name="delete_event" handler="MainWindowDeleteEvent" last_modification_time="Thu, 20 Oct 2005 13:24:07 GMT"/>

  <child>
    <widget class="GtkLabel" id="messageLabel">
      <property name="visible">True</property>
      <property name="label" translatable="yes">Message here...</property>
      <property name="use_underline">False</property>
      <property name="use_markup">False</property>
      <property name="justify">GTK_JUSTIFY_LEFT</property>
      <property name="wrap">False</property>
      <property name="selectable">False</property>
      <property name="xalign">0.5</property>
      <property name="yalign">0.5</property>
      <property name="xpad">0</property>
      <property name="ypad">0</property>
    </widget>
  </child>
</widget>

</glade-interface>

Now we need to create a SharpDevelop project to add this Glade xml file to.

Create a new empty project

  1. Select File | New | Solution to open the "New Project" dialog.
  2. Select the C# category and the "Empty Project" template. Enter in project name "HelloGtkWorld" and choose a location. Click the "Create" button to create your empty project.

Remove all unwanted assembly references

  1. Select View | Projects to open up the project browser.
  2. Expand the tree so the assembly references are displayed. There should be two, System and System.Xml. Select one at a time, right click and select "Remove". Repeat until all references have been removed.

Add a Glade xml file to the project

  1. Select the project tree node, right click and select Add | New Item... to open the "New File" dialog.
  2. Select the Misc category and the "Empty XML file" template. Type in the name "glade.xml" and click the "Create" button.
  3. Modify the glade.xml file so it contains the xml shown above.
  4. Embed this xml file as a resource. Select the file in the Project Browser. Select the menu View | Properties. Change the "Build action" to "EmbeddedResource".

Create a new C# class that will be the main entry point for the application

  1. Open the project browser again. Select the project tree node, right click and select Add | New Item... to open the "New File" dialog.
  2. Select the C# category and the Class template. Type in the filename GladeApp.cs and click the "Create" button to add the new C# class file to the project.
  3. This file should be opened when created. Modify the code so the file contains:

using Glade;
using Gtk;
using System;

public class GladeApp
{    
    [Widget]
    Label messageLabel;
       
    public static void Main (string[] args)
    {
        new GladeApp (args);
    }

    public GladeApp (string[] args
    {
        Application.Init();
        Glade.XML gxml new Glade.XML(null"HelloGtkWorld.glade.xml""mainWindow"null);
        gxml.Autoconnect(this);
        messageLabel.Text "Hello, Gtk# World.";
        Application.Run();
    }
    
    void MainWindowDeleteEvent(object oDeleteEventArgs args)
    {
        Application.Quit();
        args.RetVal true;
    }
}

Let us have a look at this code before we attempt to make sure it compiles. The main part of interest is in the GladeApp class's constructor. First Gtk is initialised:

        Application.Init();

This must be called before any Gtk libraries are used.

Then a Glade.XML object is created that will use our "glade.xml" file which has been embedded as a resource. Note that the resource name is the name of the file with the project's namespace prefixed "HelloGtkWorld.gui.xml".

        Glade.XML gxml new Glade.XML(null"HelloGtkWorld.glade.xml""mainWindow"null);

The Autoconnect call connects the signals defined in the Glade xml file to matching handlers in the GladeApp class. It will also initialise any fields that correspond to UI components defined in the Glade xml file.

        gxml.Autoconnect(this);

The next line sets the text of the label that will be displayed in the main window.

        messageLabel.Text "Hello, Gtk# World.";

The messageLabel object

    [Widget]
    Label messageLabel;

will auto-magically be initialised for us by the Glade.Xml object.

Finally the application is run via:

        Application.Run();

When the user closes the main window the MainWindowDeleteEvent method is called.

    void MainWindowDeleteEvent(object oDeleteEventArgs args)
    {
        Application.Quit();
        args.RetVal true;
    }

This method makes sure the application quits. If the Application.Quit method is not called then even though the main window has been closed the app will still be running in the background.

Add references to the Glade and Gtk assemblies.

  1. Open the Project Browser. Select the "References" node, right click and select "Add References".
  2. In the "Add Reference" dialog select the ".NET Assembly Browser" tab and click the "Browse..." button.
  3. Browse to the Mono folder containing the assemblies for Glade and Gtk#. The folder should be something like "C:\Program Files\Mono\lib\mono\gtk-sharp". Select the glade-sharp and gtk-sharp dlls and click the "Open" button. In the "Add Reference" dialog click the "OK" button.

Compiling the code

  1. Select the menu item Project | Project Options. In the "Application" tab, change the "Output type" to be "Windows Application".
  2. In the "Compiling" tab, change the "Target framework" to "Mono 1.1".
  3. In the "Debug" tab, change the "Start Action" to "Start external program". Set the external program field by browsing and selecting Mono.exe, which lives in "C:\Program Files\Mono\bin". In the "Start Options" group set the "Command line arguments to be "${TargetPath}" and the "Working directory" to "${TargetDir}".
  4. Select the menu Build | Rebuild Solution. The code should build successfully. There will be two warnings, one about MainWindowDeleteEvent method never being called and another about the messageLabel variable never being assigned to, both of which can be safely ignored.

It is optional whether you use the Mono as the Target framework, the Gtk# and Glade libraries should work under the .NET framework. You will have to add references to the Glib and Atk libraries to be able to compile using Csc.

Running the app

  1. Select the menu Debug | Run without debugger.


As with the target framework, it is optional whether you run the application under Mono since the Gtk# and Glade libraries should work under the .NET framework.

Other Useful Resources

Introduction to Mono - Your first Mono app - Covers creating a Mono console app.

A google search application using Gtk# - Uses Visual Studio to create a Gtk# Mono app that uses Google's web service.

A Gtk# beginners guide. - Covers Gtk# and Glade.

GUI development with Glade - A tutorial covering the Glade User Interface Builder.

Mono Documentation - Covers all the Mono libraries including Gtk# and Glade.

[Update: 10 Jan, 2006]

SharpDevelop2 now ships with Gtk# and Glade# templates.  Support for Mono's GAC has also been added.  Read more here.

Published Oct 20 2005, 04:40 PM by MattWard
Filed under:

Comments

 

Alessandro "jekil" Tanasi blog said:

Una lista di semplici how to ed esempi per scrivere applicazioni con Glade e C#Glade - Rapid Application DevelopmentGlade screencast using C#How to build a Glade# application that uses glade-2 with Visual Studio.Net 2003Creating a Gtk# App with SharpDeve
July 1, 2006 1:39 PM
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.