GNOME.NET

In the previous GNOME.NET tutorial, we leaned how to make a GNOME application window. However, other than being a good example to teach us GNOME.NET, that window was pretty useless, considering it didn't really do anything.

Any real application will have stuff inside the window. In this tutorial, we will show you how to give your window some contents.

Adding Contents

button.cs:

    class MainClass
    {
            static void Main(string[] args)
            {
                    Gnome.Program program =
                    new Gnome.Program("Hello World", "1.0", Gnome.Modules.UI, args);

                    MyMainWindow app = new MyMainWindow(program);
                    app.ShowAll();
 
                    program.Run();
            }
    }



    class MyMainWindow
            : Gnome.App
    {
            Gnome.Program program;

            public MyMainWindow(Gnome.Program gnome_program)
                    : base("Hello World", "Hello World")
            {
                    this.program = gnome_program;

                    this.DeleteEvent += new GtkSharp.DeleteEventHandler(delete_event);

                    this.Contents = new Gtk.Button("Hello World");
            }

            private void delete_event(object obj, GtkSharp.DeleteEventArgs args)
            {
                    this.program.Quit();
            }
    }

compile:

mcs button.cs -r gnome-sharp.dll

run:

mono button.exe

Except for one line, this code is virtually exactly like the helloworld2.cs source code from the previous tutorial. The line that adds the button is:

                    this.Contents = new Gtk.Button("Hello World");

The magic comes from the Contents property of Gnome.App (and all of Gnome.App's subclasses, like MyMainWindow).

What you assign to this goes inside the window. For example, I could put an image in there with:

                    Gtk.Image image = new Gtk.Image();

                    // put something in the image here.

                    this.Contents = image;

I could put in a Gnome.Canvas with.

                    this.Contents = new Gnome.Canvas();

(Hopefully you get the idea.) If you want to put something inside of a Gnome.App (or one of its subclasses), then assign it to its Contents property.