The absolute beginners guide - Part I - for gnu-smalltalk/gtk (Toplevel window)

Tagged:  •    •    •    •    •  

Ok, here's my first blog-entry.

Because I wanted to start with gnu-smalltalk - and smalltalk at all - I was trying to find some information on how to start. The most tutorials in the web are based on VisualWorks and are not really helpful for me, trying to start with gnu-smalltalk. Hopefully, with the GTK-Launcher http://smalltalk.gnu.org/blog/mrgwen the thing will change, but you still have to know, how to handle the GTK-Stuff with gnu-smalltalk.

And that's what the beginning of my blog is about.

My start was, to create a class, which creates only an empty GTK window on the screen. Because I want to seperate the code from the existing classes, I used an own Namespace.
(My assumption was, to use an own Namespace for my code, and to write every class into a separate file, which is named equaly to the class-name. I think, this would not be necessary in gnu-smalltalk, but it makes the collection of classes on the file-system a bit easier for me to overlook.)

After some digging into the documentation, googling and so on, I end with this as the minimal code for starting a GUI application:

MainWindow.st

Namespace current: JoesTest [
     Object subclass: MainWindow [
          <comment: 'My first GTK-Gui class'>
          | window |

          MainWindow class >> open [
               | r |
               <category: 'user interface'>

               r := self new.
               r initialize.
               r show.
               ^r
          ]

          initialize [
               <category: 'initialization'>

               window := GTK.GtkWindow new: GTK.Gtk gtkWindowToplevel
          ]

          show [
               window show
          ]
     ]
]

(There should be nothing to complicated in it - besides the GTK-Part, which I just copied from the source of the GTK-Launcher. GTK is additionally a bit new to me, but that's another story.)

after reading the gnu-smalltalk tutorial, you would know, that the Class-Method "open" could also be written like the following:

     MainWindow class >> open [
          <category: 'user interface'>

          ^(self new) initialize;show
     ]

but my first try should be as easy to understand as possible.

So now I had my first class, which should hopefully display an empty GTK-Window on the screen. But how to start the "application" or how to proceed from now on...

I looked into the source of the GTK-Launcher and found the Load.st script, which I adapted to my needs.

Load.st

Eval [
     PackageLoader fileInPackage: 'GTK'.
     FileStream fileIn: 'MainWindow.st'.
     GTK.Gtk gstGtkInit.
     JoesTest.MainWindow open.
     GTK.Gtk main
]

(* the Eval is not realy neccesarry, maybe someone could give me a tip, for which this is helpfull)
  • The PackageLoader loads the GTK-Package, which is - of course - a prerequesite for GTK-Programm, into the image.
  • The FileStream loads a single source File into the image and
  • The rest intantiates my class and goes into the GTK-MainLoop.

After all this editing I was succesfully with the start command:

gst -f Load.st

Ok, it took me a littel time to find all this, especially the FileStream fileIn: thing, to load my Class-File into the environment. But that's not the end of the whole (first) story.

If you would like to do scripting with gnu-smalltalk, it would be unattractive, to have always 2 scripts, where one of them is only for starting the real script. So I tried to pack them together. I ended here with the following:

  • put the PackageLoad of the GTK to the beginning of the class-file and
  • the rest after the class-definition.

then you could start the script with:

gst -f MainWindow.st

and even this could be made simpler by putting the hashbang code into the first line of the script

#!/usr/local/bin/gst -f

and making the script executable.

After all this, you could start the script only by the name and be happy about the first gnu-smalltalk app.

And for the end now, here's the script where I end with. Hopefully it helps a bit for your start!

Happy coding!
Joachim.

MainWindow.st

#!/usr/local/bin/gst -f

PackageLoader fileInPackage: 'GTK'.

Namespace current: JoesTest [
        Object subclass: MainWindow [
                <comment: 'This is my first GTK-GUI st-class'>
                | window |

                MainWindow class >> open [
                        | r |
                        <category: 'user interface'>

                        r := self new.
                        r initialize.
                        r show.
                        ^r
                ]

                initialize [
                        <category: 'initialization'>

                        window := GTK.GtkWindow new: GTK.Gtk gtkWindowToplevel.
                        window setTitle: 'My first GNU-smalltalk GUI'.
                        window resize: 400 height: 300.

                        "And if you would like to place the window to a"
                        "specified point on the screen,"
                        "use the following:"

                        window move: 100 y: 100
                ]

                show [
                        window showAll
                ]

        ]
]

GTK.Gtk gstGtkInit.
JoesTest.MainWindow open.
GTK.Gtk main

User login