[Gtk-sharp-list] MonkeyGuide?: Hello World in GNOME.NET - Fourth Draft
Charles Iliya Krempeaux
charles@reptile.ca
19 Feb 2003 23:05:19 -0800
--=-QoPPB0aTAh0b/RSb/XlT
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
Hello,
First, this is being cross posted to both the Gtk# Mailing List and the
Mono Docs Mailing List. So make sure to do a "Reply to All" when
replying to this. (So that everyone can keep up with the conversation.)
This is my fourth draft of the GNOME.NET HelloWorld tutorial, for the
MonkeyGuide.
The first draft has already been posted in the MonekyGuide, and can be
found at:
http://go-mono.com/tutorial/html/en/gnome/bindings/gnome/hello_world.html
This make one correction.
(Some one else, with the ability, will again need to add this to the
MonkeyGuide.)
Here's the first tutorial, GNOME.NET Hello World:
------- BEGIN -------
GNOME.NET
This tutorial gets you started with writing GNOME.NET programs.
HelloWorld, first try
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.Show();
program.Run();
}
}
compile:
mcs helloworld1.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.Show();
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.
HelloWorld, second try
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.Show();
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.
-------- END --------
See ya
--
Charles Iliya Krempeaux, BSc
charles@reptile.ca
________________________________________________________________________
Reptile Consulting & Services 604-REPTILE http://www.reptile.ca/
--=-QoPPB0aTAh0b/RSb/XlT
Content-Type: text/html; charset=utf-8
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<META NAME="GENERATOR" CONTENT="GtkHTML/1.1.8">
</HEAD>
<BODY>
Hello,<BR>
<BR>
First, this is being cross posted to both the Gtk# Mailing List and the Mono Docs Mailing List. So make sure to do a "Reply to All" when replying to this. (So that everyone can keep up with the conversation.)<BR>
<BR>
This is my fourth draft of the GNOME.NET HelloWorld tutorial, for the MonkeyGuide.<BR>
<BR>
The first draft has already been posted in the MonekyGuide, and can be found at:<BR>
<BR>
<A HREF="http://go-mono.com/tutorial/html/en/gnome/bindings/gnome/hello_world.html"><FONT SIZE="3"><TT>http://go-mono.com/tutorial/html/en/gnome/bindings/gnome/hello_world.html</TT></FONT></A><BR>
<BR>
This make one correction.<BR>
<BR>
(Some one else, with the ability, will again need to add this to the MonkeyGuide.)<BR>
<BR>
<BR>
Here's the first tutorial, GNOME.NET Hello World:<BR>
<BR>
<FONT SIZE="3"><TT>------- BEGIN -------</TT></FONT><BR>
<BR>
<BR>
<BR>
<BR>
<H1>GNOME.NET</H1>
This tutorial gets you started with writing GNOME.NET programs.
<H2>HelloWorld, first try</H2>
<FONT SIZE="3"><TT>helloworld1.cs</TT></FONT>:<BR>
<BR>
<TABLE BGCOLOR="#bfbfbf" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<TABLE BGCOLOR="#f2f2f2" CELLSPACING="1" CELLPADDING="3" WIDTH="100%">
<TR>
<TD BGCOLOR="#d5d5d5">
<FONT SIZE="3"><TT> class HelloWorld<BR>
{<BR>
static void Main(string[] args)<BR>
{<BR>
Gnome.Program program =<BR>
new Gnome.Program("Hello World", "1.0", Gnome.Modules.UI, args);<BR>
<BR>
Gnome.App app = new Gnome.App("Hello World", "Hello World");<BR>
app.Show();<BR>
<BR>
program.Run();<BR>
}<BR>
}</TT></FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<BR>
compile:<BR>
<BR>
<TABLE BGCOLOR="#bfbfbf" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<TABLE BGCOLOR="#f2f2f2" CELLSPACING="1" CELLPADDING="3" WIDTH="100%">
<TR>
<TD BGCOLOR="#808080">
<FONT COLOR="#ffffff" SIZE="3"><TT>mcs helloworld1.cs -r gnome-sharp.dll</TT></FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<BR>
run:<BR>
<BR>
<TABLE BGCOLOR="#bfbfbf" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<TABLE BGCOLOR="#f2f2f2" CELLSPACING="1" CELLPADDING="3" WIDTH="100%">
<TR>
<TD BGCOLOR="#808080">
<FONT COLOR="#ffffff" SIZE="3"><TT>mono helloworld1.exe</TT></FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<BR>
It's a bit longer than console Hello World and needs some explanation.<BR>
<BR>
In <FONT SIZE="3"><TT>Main()</TT></FONT> at first you see:<BR>
<BR>
<TABLE BGCOLOR="#bfbfbf" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<TABLE BGCOLOR="#f2f2f2" CELLSPACING="1" CELLPADDING="3" WIDTH="100%">
<TR>
<TD BGCOLOR="#d4d4d4">
<FONT SIZE="3"><TT> Gnome.Program program =<BR>
new Gnome.Program("Hello World", "1.0", Gnome.Modules.UI, args);</TT></FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<BR>
This initializes GNOME and is needed in every GNOME application. Here we are creating a variable of type <FONT SIZE="3"><TT>Gnome.Program</TT></FONT>, called <FONT SIZE="3"><TT>program</TT></FONT>. This variable, <FONT SIZE="3"><TT>program</TT></FONT>, is used to control the GNOME program, as we'll see later.<BR>
<BR>
Next we see:<BR>
<BR>
<TABLE BGCOLOR="#bfbfbf" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<TABLE BGCOLOR="#f2f2f2" CELLSPACING="1" CELLPADDING="3" WIDTH="100%">
<TR>
<TD BGCOLOR="#d4d4d4">
<FONT SIZE="3"><TT> Gnome.App app = new Gnome.App("Hello World", "Hello World");</TT></FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<BR>
This creates our GNOME application window. (That's what you see on the screen.)<BR>
<BR>
We then see:<BR>
<BR>
<TABLE BGCOLOR="#bfbfbf" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<TABLE BGCOLOR="#f2f2f2" CELLSPACING="1" CELLPADDING="3" WIDTH="100%">
<TR>
<TD BGCOLOR="#d4d4d4">
<FONT SIZE="3"><TT> app.Show();</TT></FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<BR>
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.<BR>
<BR>
And finally we see:<BR>
<BR>
<TABLE BGCOLOR="#bfbfbf" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<TABLE BGCOLOR="#f2f2f2" CELLSPACING="1" CELLPADDING="3" WIDTH="100%">
<TR>
<TD BGCOLOR="#d4d4d4">
<FONT SIZE="3"><TT> program.Run();</TT></FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<BR>
This make your GNOME program <FONT SIZE="3"><I>run</I></FONT>. 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.<BR>
<BR>
<BR>
<BR>
<H2>HelloWorld, second try</H2>
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.)<BR>
<BR>
<FONT SIZE="3"><TT>helloworld2.cs</TT></FONT>:<BR>
<BR>
<TABLE BGCOLOR="#bfbfbf" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<TABLE BGCOLOR="#f2f2f2" CELLSPACING="1" CELLPADDING="3" WIDTH="100%">
<TR>
<TD BGCOLOR="#d5d5d5">
<FONT SIZE="3"><TT> class HelloWorld<BR>
{<BR>
static void Main(string[] args)<BR>
{<BR>
Gnome.Program program =<BR>
new Gnome.Program("Hello World", "1.0", Gnome.Modules.UI, args);<BR>
<BR>
MyMainWindow app = new MyMainWindow(program);<BR>
app.Show();<BR>
<BR>
program.Run();<BR>
}<BR>
}<BR>
<BR>
<BR>
<BR>
class MyMainWindow<BR>
: Gnome.App<BR>
{<BR>
Gnome.Program program;<BR>
<BR>
public MyMainWindow(Gnome.Program gnome_program)<BR>
: base("Hello World", "Hello World")<BR>
{<BR>
this.program = gnome_program;<BR>
<BR>
this.DeleteEvent += new GtkSharp.DeleteEventHandler(delete_event);<BR>
}<BR>
<BR>
private void delete_event(object obj, GtkSharp.DeleteEventArgs args)<BR>
{<BR>
this.program.Quit();<BR>
}<BR>
}</TT></FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<BR>
compile:<BR>
<BR>
<TABLE BGCOLOR="#bfbfbf" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<TABLE BGCOLOR="#f2f2f2" CELLSPACING="1" CELLPADDING="3" WIDTH="100%">
<TR>
<TD BGCOLOR="#808080">
<FONT COLOR="#ffffff" SIZE="3"><TT>mcs helloworld2.cs -r gnome-sharp.dll -r gtk-sharp.dll</TT></FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<BR>
run:<BR>
<BR>
<TABLE BGCOLOR="#bfbfbf" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<TABLE BGCOLOR="#f2f2f2" CELLSPACING="1" CELLPADDING="3" WIDTH="100%">
<TR>
<TD BGCOLOR="#808080">
<FONT COLOR="#ffffff" SIZE="3"><TT>mono helloworld2.exe</TT></FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<BR>
The first thing you probably have noticed is that there are now two classes. The first class -- <FONT SIZE="3"><TT>HelloWorld</TT></FONT> -- is almost identical to the version in the previous example. Except for the following line:<BR>
<BR>
<TABLE BGCOLOR="#bfbfbf" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<TABLE BGCOLOR="#f2f2f2" CELLSPACING="1" CELLPADDING="3" WIDTH="100%">
<TR>
<TD BGCOLOR="#d5d5d5">
<FONT SIZE="3"><TT> MyMainWindow app = new MyMainWindow(program);</TT></FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<BR>
You can compare this to the equivalent line in the previous code:<BR>
<BR>
<TABLE BGCOLOR="#bfbfbf" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<TABLE BGCOLOR="#f2f2f2" CELLSPACING="1" CELLPADDING="3" WIDTH="100%">
<TR>
<TD BGCOLOR="#d5d5d5">
<FONT SIZE="3"><TT> Gnome.App app = new Gnome.App("Hello World", "Hello World");</TT></FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<BR>
The difference is that <FONT SIZE="3"><TT>app</TT></FONT> is no longer an instance of <FONT SIZE="3"><TT>Gnome.App</TT></FONT>, as it was in the first example <FONT SIZE="3"><TT>helloworld.cs</TT></FONT>. But is now an instance of a new class that we just created: <FONT SIZE="3"><TT>MyMainWindow</TT></FONT>. (Also, what we pass to the constructor of <FONT SIZE="3"><TT>MyMainWindow</TT></FONT> is different from what we passed to the constructor of <FONT SIZE="3"><TT>Gnome.App</TT></FONT>.)<BR>
<BR>
In other words, the only difference (in this class) is that we are using a different class for our window.<BR>
<BR>
Now let us take a look at our <FONT SIZE="3"><TT>MyMainWindow</TT></FONT> class.<BR>
<BR>
We created this new class because <FONT SIZE="3"><TT>Gnome.App</TT></FONT> 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 <FONT SIZE="3"><TT>MyMainWindow</TT></FONT> class accomplishes.<BR>
<BR>
The important thing to focus on is this line:<BR>
<BR>
<TABLE BGCOLOR="#bfbfbf" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<TABLE BGCOLOR="#f2f2f2" CELLSPACING="1" CELLPADDING="3" WIDTH="100%">
<TR>
<TD BGCOLOR="#d5d5d5">
<FONT SIZE="3"><TT> this.DeleteEvent += new GtkSharp.DeleteEventHandler(delete_event);</TT></FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<BR>
This makes it so that when the "X" button on the title bar is pressed, the function <FONT SIZE="3"><TT>delete_event</TT></FONT> is called. Technically, when the "X" button is pressed, the program receives a <FONT SIZE="3"><TT>DeleteEvent</TT></FONT>, which is what you see in the code above.<BR>
<BR>
The next thing to focus on is the definition of <FONT SIZE="3"><TT>delete_event</TT></FONT>:<BR>
<BR>
<TABLE BGCOLOR="#bfbfbf" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<TABLE BGCOLOR="#f2f2f2" CELLSPACING="1" CELLPADDING="3" WIDTH="100%">
<TR>
<TD BGCOLOR="#d5d5d5">
<FONT SIZE="3"><TT> private void delete_event(object obj, GtkSharp.DeleteEventArgs args)<BR>
{<BR>
this.program.Quit();<BR>
}</TT></FONT>
</TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE>
<BR>
<BR>
This is the function that is called when the application receives a <FONT SIZE="3"><TT>DeleteEvent</TT></FONT>. 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.<BR>
<BR>
Which is what we wanted.<BR>
<FONT SIZE="3"><TT><BR>
-------- END --------</TT></FONT><BR>
<BR>
<BR>
<BR>
See ya<BR>
<BR>
<TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<PRE>--
Charles Iliya Krempeaux, BSc
charles@reptile.ca
________________________________________________________________________
Reptile Consulting & Services 604-REPTILE http://www.reptile.ca/</PRE>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>
--=-QoPPB0aTAh0b/RSb/XlT--