This tutorial gets you started with writing GNOME.NET programs.
helloworld1.cs
:
class HelloWorld { static void Main(string[] args) { Gnome.Program program = new Gnome.Program("Hello World", "1.0", Gnome.Modules.UI, args); Gnome.App app = new Gnome.App("Hello World", "Hello World"); app.ShowAll(); program.Run(); } }
compile:
mcs helloworld.cs -r gnome-sharp.dll
run:
mono helloworld1.exe
It's a bit longer than console Hello World and needs some explanation.
In Main()
at first you see:
Gnome.Program program = new Gnome.Program("Hello World", "1.0", Gnome.Modules.UI, args);
This initializes GNOME and is needed in every GNOME application.
Here we are creating a variable of type Gnome.Program
,
called program
. This variable, program
,
is used to control the GNOME program, as we'll see later.
Next we see:
Gnome.App app = new Gnome.App("Hello World", "Hello World");
This creates our GNOME application window. (That's what you see on the screen.)
We then see:
app.ShowAll();
This makes the GNOME Application Window (that you created with the previous line of code) visible on the screen. With GNOME, things don't automatically display themselves unless you explicitly tell them too.
And finally we see:
program.Run();
This make your GNOME program run. It makes all the magic happen, that you don't need to worry about at this moment. Needless to say though, you need to do this to make your GNOME Application work.
While the above program compiles and runs, it's doesn't quit, properly. You have to exit by pressing CTRL+C. (Ideally, we want the program to close when you press the "X" on the title bar. Which is what this next example does.)
helloworld2.cs
:
class HelloWorld { 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); } private void delete_event(object obj, GtkSharp.DeleteEventArgs args) { this.program.Quit(); } }
compile:
mcs helloworld2.cs -r gnome-sharp.dll -r gtk-sharp.dll
run:
mono helloworld2.exe
The first thing you probably have noticed is that there are now
two classes. The first class -- HelloWorld
-- is
almost identical to the version in the previous example. Except for
the following line:
MyMainWindow app = new MyMainWindow(program);
You can compare this to the equivalent line in the previous code:
Gnome.App app = new Gnome.App("Hello World", "Hello World");
The difference is that app is no longer an instance of
Gnome.App
, as it was in the first example
helloworld.cs
. But is now an instance of a new class
that we just created: MyMainWindow
. (Also, what we
pass to the constructor of MyMainWindow
is different
from what we passed to the constructor of
Gnome.App
.)
In other words, the only difference (in this class) is that we are using a different class for our window.
Now let us take a look at our MyMainWindow
class.
We created this new class because Gnome.App did not do exactly
what we wanted it to. We want our application window to close when
the "X" on the title bar is pressed. Which is what the code in the
MyMainWindow
class accomplishes.
The important thing to focus on is this line:
this.DeleteEvent += new GtkSharp.DeleteEventHandler(delete_event);
This makes it so that when the "X" button on the title bar is
pressed, the function delete_event
is called.
Technically, when the "X" button is pressed, the program receives a
DeleteEvent
, which is what you see in the code
above.
The next thing to focus on is the definition of
delete_event
:
private void delete_event(object obj, GtkSharp.DeleteEventArgs args) { this.program.Quit(); }
This is the function that is called when the application
receives a DeleteEvent
. I.e., when the user clicks the
"X" button on the title bar. As you can hopefully see, when this
function is called, the program will quit.
Which is what we wanted.