FXRuby :: Hello, World!

>> Tuesday, May 11, 2010

Hello! And welcome to the first tutorial! In these tutorials I'll try to explain how to use FXRuby in an efficient Ruby-ish way, and try to show how easy and useful it is! In each tutorial we are going to make a program to explain the tutorial topics better, I'm a fan of real-life examples, and although we are not going to make complete applications, the programs will show FXRuby features nicely (I hope!).

In this first tutorial I'll explain the very basics about FXRuby, and how to make the most simplistic program, the "Hello, World!". I'll assume normal Ruby knowledge. If you don't know Ruby or wanna refresh some stuff, here is a free online book, and here is another :).
Okay, let's begin, first of all you have to install FXRuby, the good news are, that FXRuby is packed with the standard ruby distribution, so if you have ruby you already have FXRuby :)
Let's begin doing the almost mandatory "Hello, World!" program. That program is normally a program which only output is "Hello, World!".
As we are doing GUI here, we could translate that into a window which writes "Hello, World!" somewhere in there, so let's do that.

#!/usr/bin/env ruby

require 'fox16'

app = Fox::FXApp.new

mainWindow = Fox::FXMainWindow.new(app, "Hello, World!", :width => 300, :height => 200)
mainWindow.show(Fox::PLACEMENT_SCREEN)

app.create
app.run

When you run that code, you'll see something like this.


That's it, we made a window, and if you have worked with some other GUI frameworks you'll know that, that was pretty easy :)
Let's review the code, first of all, we require fox16, which is the gem we are going to use.
All Fox applications have one FXApp object, which is the actual application, we are gonna add all the "stuff" there.
Next we create a FXMainWindow, as you can see by looking at the documentation, it has some optional parameters, but the required ones are app, and title, as a title we used "Hello, World!".

Note for Java programmers: One thing that makes different Fox from Swing, is that, in Swing, you have a parent controller and you add a child to the parent controller, in Fox, you specify the parent of the child controller, and that's how you add new components.

FXMainWindow also has some optional parameters, we sent :width and :height in order to specify...well...the window width and height :)
By default all windows are hidden, so we call the show method of the FXMainWindow, we pass the PLACENEMT_SCREEN argument just so it centers on the screen.

The next lines of code are required in order to run the application, I'll explain them in more detail later on, but by now, just know that you'll need to call create and run.

That works great for what we want to do, but it's not a very Ruby-ish way, we could write exactly the same with this code

#!/usr/bin/env ruby

require 'fox16'
include Fox

class HelloWorldWindow < FXMainWindow
  def initialize(app)
    super(app, "Hello, World!", :width => 300, :height => 200)
  end
  
  def create
    super
    show(PLACEMENT_SCREEN)
  end
end

if __FILE__ == $0
  app = FXApp.new
  HelloWorldWindow.new(app)
  app.create
  app.run
end

As you can see, it looks like a normal Ruby program, but one thing is very important, note we can inherit from the FXMainWindow class, if you have done Swing, this will look very familiar, and it's a very good thing! That means we can extend the base class and add our own functionality to each of Fox's widgets, in a very easy way.
If you run the program now, you'll see exactly the same output, the same window we saw before.
Finally, let's just add a label with the "Hello, World!" text, we can add a label using the FXLabel class, in this case we just want the default behaviour so we wont create a new class for the label.

#!/usr/bin/env ruby

require 'fox16'
include Fox

class HelloWorldWindow < FXMainWindow
  def initialize(app)
    super(app, "Hello, World!", :width => 300, :height => 200)
    FXLabel.new(self, "Hello, World!")
  end
  
  def create
    super
    show(PLACEMENT_SCREEN)
  end
end

if __FILE__ == $0
  app = FXApp.new
  HelloWorldWindow.new(app)
  app.create
  app.run
end

If you look at the documentation for the FXLabel class you'll know we just need to send the parent widget and the text we want to display as parameters.
If you run the program you'll see something like this


We have done our "Hello, World!" program in a very extensible and object oriented way, a very Ruby-ish way, that will allow us to extend the application very easily.
That's all for this tutorial, hope you find it useful! No downloads for this tutorial as it's only the source code you can find above.

Post a Comment

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP