Mono is a well-supported platform for the C# language with a lot of libraries for GUI programming available. Naturally, most of these are bindings and/or wrappers to popular GUI tool kits. Gtk# (pronounced "GTK Sharp") is the name of the GTK+ bindings for C#.

If you are new to programming in C#, you can find some excellent documents and tutorials at the Mono website and the MSDN C# Tutorials page.

Your First GTK# Script

The goal of this first introduction is to show you a very basic, easy-to-understand "Hello World!" example for Gtk#.

First Importing ...

using Gtk;
using Gdk;
using Glade;

Anytime you wish to use Gtk#, you need to import the Gtk namespace. Most of the time, you won't need to import the Gdk or Glade namespaces.

Example 1

Here's an example of a "Hello World!" application which only requires the Gtk namespace.

// file: helloworld.cs
using Gtk;
using System;

class Hello {

        static void Main()
        {
                //Run this always!
                Application.Init ();

                //Create our Window
                Window myWindow = new Window("Hello World!");
                window.Resize(200,200);
                
                //Create a Label and put some text in it
                Label myLabel  = new Label();
                myLabel.Text = "Hello World! I'm here!!!";

                //Add the label to the form
                myWindow.Add(myLabel);

                //Show to the world
                myWindow.ShowAll();

                //Let's do this!
                Application.Run();
        }
}

To compile the above example, just run the following code:

mcs helloworld.cs -pkg:gtk-sharp-2.0

Let's walk through the code first though!

Application.Init ();

The above code is used to initialize GTK and is required in every Gtk# application. No exceptions.

This next chunk of code creates the window object, wherein the first parameter is the title of the window. Then we can make it display by calling ShowAll():

Window myWindow = new Window ("Hello World!");
myWindow.ShowAll();

Finally, we run the main application loop - this will display everything for us and wait for an event, or until we call Quit():

Application.Run();

Now go ahead and run the program, which you compiled earlier. Note that you'll have to exit the program via CTRL-C in the terminal, since the code to let the application stop itself isn't there yet.

What Else Can I Do With Gtk#?

This section will walk you through using call-backs and connecting them to signals.

What Are Call-Backs and Signals, and Why Should I Care?

Gtk# relies on the design concept that it should stay out of your hair until you tell it what to do. We do this through the use of signals, which are kind of like any other signal you might see in real life: like a pager, or a cellphone going off. In Gtk#, you attach a signal to any number of things. This signal then waits for a specific event - say a button being clicked - and once it does, the signal sends a message to Gtk# telling it to call the call-back function.

Call-back functions are the directions that Gtk# uses to make the application do what your signal told it to. Call-back function will never be run on their own: they always require a signal in order to execute.

Example 2

// file: helloworld2.cs
using Gtk;
using System;
 
class Hello {
 
        static void Main()
        {
                Application.Init ();
 
                // Set up a button object.
                Button myButton = new Button("Hello World");
                // when this button is clicked, it'll run hello()
                myButton.Clicked += new EventHandler (hello);
 
                Window myWindow = new Window("helloworld");
                // when this window is deleted, it'll run delete_event()
                myWindow.DeleteEvent += delete_event;
                        
                // Add the button to the window and display everything
                myWindow.Add(myButton);
                myWindow.ShowAll ();
 
                Application.Run ();
        }
 
 
        // runs when the user deletes the window using the "close
        // window" widget in the window frame.
        static void delete_event (object obj, DeleteEventArgs args)
        {
                Application.Quit ();
        }
 
        // runs when the button is clicked.
        static void hello (object obj, EventArgs args)
        {
                Console.WriteLine("Hello World");
                Application.Quit ();
        }
}

Even though the above code features some very nifty comments, some things still need a little more explaining, so here we go.

Gtk# is an event-driven toolkit: it will "sleep" in Application.Run() until an event occurs, and a signal is sent to run the call-back function. In order to connect your application to an event, you must connect it to the event handler, like we did with the following:

myWindow.DeleteEvent += delete_event;

Now, when you close the "HelloWorld" window, the window throws an event of type DeleteEvent.

Event handlers typically get passed four parameters (arguments): an object, the object that fired the event, the window, and an EventArgs object:

                static void delete_event (object obj, DeleteEventArgs args)
                {
                            Application.Quit ();
                }

In the above case, the EventArgs have the special type DeleteEventArgs.

Isn't There Another Way To Do This?

Of course there is! Many developers of GTK+ applications use an application called Glade to make their UIs.

Why Glade?

Glade is often cited as a quick and easy solution for making GTK+ applications by allowing the developer to drag-and-drop his interface together, and then attach the code to it later.

Glade creates an XML file, which Gtk# can use to handle the window.

Show Me The Code!

For purposes of this example, this shall be our handy-dandy Glade file. http://live.gnome.org/GtkSharpQuickIntro?action=AttachFile&do=get&target=glade.png

This code contains every bit of information Gtk# will need to make your application.

How To Make It Work

Assuming you've saved the above as something easy to remember (for this example, gui.glade), it should contain definitions for a window named window1, a button called button1, etc.

// file: glade.cs
using System;
using Gtk;
using Glade;

public class GladeApp
{
        public static void Main (string[] args)
        {
                new GladeApp (args);
        }
 
        public GladeApp (string[] args) 
        {
                Application.Init();

                // Load 'window1' from file 'gui.glade'
                Glade.XML gxml = new Glade.XML (null, "gui.glade", "window1", null);
                gxml.Autoconnect (this);

                Application.Run();
        }
}

To compile the above code, you need to use the following:

mcs -pkg:glade-sharp -resource:gui.glade glade.cs

A video demonstration for creating a Glade# (that means Gtk# using Glade) application can be found here, courtesy of Nat Friedman.

Mono Develop

Mono Develop is an advanced IDE for Mono which has a great deal of built-in support for handling Glade interfaces in a way that's more specific to Gtk#. Many developers use it for designing applications and the UIs that go with them.

Is That It?

For now, I'm afraid it is. This tutorial was meant to provide a brief, but informative dive into developing Gtk# applications.

If you're serious about developing Gtk# applications, however, there is a plethora of information available at your fingertips:

* Mono Documentation * Gtk# Homepage


CategoryDeveloperTutorial

Attic/GtkSharpQuickIntro (last edited 2013-11-23 00:00:04 by WilliamJonMcCann)