WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Using the Gtk Toolkit with Mono

by Niel Bornstein, coauthor of Mono: A Developer's Notebook.
08/09/2004

Gtk, the Gimp Toolkit, was developed as the graphical user interface framework for the Gnu Image Manipulation Program, or Gimp. As a cross-platform UI framework, Gtk allows you to develop graphical user interfaces for applications on Microsoft Windows, various flavors of Unix and Linux, and Mac OS X, without having to write operating-system-specific UI code. Because it is cross-platform and object-oriented, the Mono team decided to use Gtk as the basis for their UI framework. Gtk#, the C# wrapper for Gtk, is the result.

Before the Mono 1.0 release, you would have had to download a number of prerequisites to develop with Gtk# in Windows. You needed various libraries that don't normally come with Windows (such as Gtk, GLib, and Pango, the Cygwin shell and utilities), and a good bit of patience. Even if you had everything you needed to develop on Linux, you still needed to do a lot of work just to deploy a Gtk# application on Windows.

Now, however, the Mono team has released a Mono and Gtk# installer for Windows that provides all of the prerequisites. Furthermore, you can use Gtk# to develop applications in either Mono on any OS, or Microsoft .NET on Windows. In this article, I'll show you how to write your first Gtk# program and deploy it in a Windows environment.

Related Reading

Mono: A Developer's Notebook
By Edd Dumbill, Niel Bornstein

How Do I Do That?

To install Mono on Windows, you can download the Win32 installer from the Mono homepage at www.mono-project.com. This installer does not include Gtk#, however, so you'll need to download the file Mono Win32 Combined Installer from forge.novell.com/modules/xfmod/project/?prj2make-sharp. You could also build a development version from CVS yourself, but that requires all of the prerequisites I mentioned above. Refer to Chapter 8, "Cutting Edge Mono," of Mono: A Developer's Notebook for more information on this option.

If you have already installed an earlier version of Mono, or Mono 1.0 without Gtk#, you should be sure to uninstall it before running the combined installer.

Mono is licensed under the GNU General Public License, and the installer will ask you to indicate your acceptance of the license. It will then place all of the necessary files in C:\Program Files\Mono-1.0\, by default, although you can specify an alternate location. This installer includes the Mono runtime and the Gtk# libraries. You'll also get a folder in your Start Menu's Programs group containing shortcuts to various Mono and Gtk# utilities and web sites.

screenshot.
Figure 1: Hello World with Gtk

Example 1 below shows code for a simple "Hello, World" program that uses Gtk# to display a window on the screen with few simple controls (called widgets in Gtk#).

using System;
using Gtk;

public class SimpleApp
{
  public static void Main()
  {
    Application.Init();

    Window window = new Window("Simple Application");

    Label label = new Label("Name");
    Entry entry = new Entry();
    Button button = new Button("Hello!");
    
    window.DeleteEvent += 
      new DeleteEventHandler(window_DeleteEvent);
    button.Clicked += new EventHandler(button_Clicked);

    VBox vbox = new VBox();

    HBox hbox = new HBox();
    hbox.PackStart(label, false, false, 12);
    hbox.PackStart(entry, false, false, 12);
    vbox.PackStart(hbox);
    vbox.PackStart(button, false, false, 12);

    window.Add(vbox);
    window.SetDefaultSize(200, 100);
    window.ShowAll();

    Application.Run();
  }

  static void window_DeleteEvent(object o, 
                                 DeleteEventArgs args)
  {
    Application.Quit();
    args.RetVal = true;
  }

  static void button_Clicked(object sender, EventArgs e)
  {
    Console.WriteLine("Button clicked");
  }
}

Gtk# is different from the WinForms framework that Microsoft .NET developers are accustomed to, although it should not be difficult to get accustomed to it. One major difference you'll discover quickly is that specifying explicit sizes and coordinates for your widgets is frowned upon in Gtk#. The GNOME project has an extensive set of human interface guidelines that you should keep in mind when designing your Gtk# application.

If you're using the Mono compiler mcs, you can reference Gtk# with the -pkg-config command-line option. If you prefer the Microsoft csc compiler, you'll have to use the /lib and /references command-line options, or add a reference to the necessary libraries in Visual Studio .NET.

To compile SimpleApp.cs in Mono, type this at the command line:

mcs -pkg:gtk-sharp -target:winexe SimpleApp.cs

To compile SimpleApp.cs using the Microsoft .NET Framework SDK, type this at the command line:

csc /lib:"C:\Program Files\Mono-1.0\lib\mono\gtk-sharp /r:gtk-sharp /r:atk-sharp /r:gdk-sharp /r:glib-sharp /target:winexe SimpleApp.cs

If you're using Visual Studio .NET, you simply need to create a project and add references to the files gtk-sharp.dll, atk-sharp.dll, gdk-sharp.dll, and glib-sharp.dll, by browsing to C:\Program Files\Mono-1.0\lib\mono\gtk-sharp.

Either way, you'll end up with an executable file called SimpleApp.exe. In the .NET framework, you can type its name at the command line or double-click on it, and it will run. To use Mono as the execution environment, type mono SimpleApp.exe at the command line.

Note that you will need to copy the referenced DLLs to the same directory as the executable for it run, since they are not installed in the Global Assembly Cache, or GAC. To learn more about the GAC, refer to .NET Framework Essentials, by Hoang Lam and Thuan L. Thai.

You can run the application on your own machine, but you've got the whole Mono and Gtk# development environment installed already. You've got two choices for deployment to user machines. If you users don't have either the .NET runtime or Mono installed, you can have them install the Mono as outlined above.

If your users already have the .NET runtime installed, however, you may choose to allow them to run your Gtk# application in .NET. In that case, they'll need to download GTK-Sharp Runtime from forge.novell.com/modules/xfmod/project?gtk-sharp. The .zip file contains a Windows Installer Package named GTK Sharp Runtime for Windows.msi. After indicating your acceptance of the GNU General Public License, you will be prompted to select the install location for the Gtk# runtime. By default, the location is C:\novell\ndk\GTK Sharp Runtime for Windows\. The only thing the installer will put in that directory, though, is the license file. The libraries themselves will be installed in the GAC.

One note about the Gtk# Windows runtime: the current version contains Gtk# 0.93, which appears to have a few quirks. I'd recommend using the combined Mono/Gtk# installer when possible.

Now that you've verified the correct installation, you're good to go. You can put the SimpleApp.exe executable anywhere on your machine and run it.

Where to Learn More

The first source of information on developing with Gtk# is the Mono documentation web site at www.go-mono.com/docs. Of course, I hope that you'll turn to Chapters 4 and 5 ("Gtk#" and "Advanced Gtk#", respectively) of Mono: A Developer's Notebook, as well.

Niel Bornstein is a Senior Architect for Novell's Systems and Resource Management Business Unit specializing in data center automation.


Return to ONDotnet.com