Let's start by getting the obligatory "Hello, World!" example out of the way. For reference, this program is included in the examples subdirectory of the standard FXRuby source code distribution. Here's the code:
require "fox" include Fox application = FXApp.new("Hello", "FoxTest") application.init(ARGV) main = FXMainWindow.new(application, "Hello", nil, nil, DECOR_ALL) FXButton.new(main, "&Hello, World!", nil, application, FXApp::ID_QUIT) application.create main.show(PLACEMENT_SCREEN) application.run |
and here's what you'll get for your trouble:
This snapshot shows the program running under Windows 2000, but it should look very similar running under X on Linux/Unix. Now let's take a line-by-line look at the program to see how it works.
We start by requiring and then including the Fox module (which goes by the feature name "fox"):
require "fox" include Fox |
The Fox module is implemented as a C++ extension module for Ruby and so it lives in a shared object file, usually named fox.so -- don't bother looking for a fox.rb file anywhere. All of the constants and classes defined by FXRuby live in the Fox module; if you decide to leave out the include Fox statement, you will of course need to prefix the class and constant names with the Fox:: qualifier.
Every FXRuby application begins by creating an application (FXApp) object, with a specified application name and vendor key:
application = FXApp.new("Hello", "FoxTest") |
These strings are used by FOX's registry mechanism, so it's important to select a meaningful and unique combination of application name and vendor key for your program. Note that there's no conflict if your application name matches someone else's application name, as long as the vendor keys are different. All of the FXRuby example programs use the vendor key "FoxTest" to avoid conflicts with your applications.
main = FXMainWindow.new(application, "Hello", nil, nil, DECOR_ALL) |
Every FXRuby application will also create a main window object, although you'll almost always subclass your program's main window from the basic FXMainWindow class provided by FOX. Indeed, you will find that most of the example programs distributed with FXRuby (in the examples directory derive their main window class from FXMainWindow. For this simple example, however, the basic functionality will suffice.
FXButton.new(main, "&Hello, World!", nil, application, FXApp::ID_QUIT) |
FOX builds up complex user interfaces using the traditional approach of placing "child" widgets inside "parent" container widgets. Here, we're constructing a new button widget (i.e. an instance of class FXButton) and the first argument to FXButton.new specifies the parent widget for the button. The second and third arguments to FXButton's constructor specify the button text and icon. The presence of the ampersand before the letter "H" in the button's text "Hello, World!" causes FOX to create a hotkey for this button. The fourth and fifth arguments specify the message target and message identifier for the button. We'll cover this topic more later, but for now just understand that the button is going to send any messages it generates to the application object, and the message identifier it will pass along with those messages is the application class' ID_QUIT message.
application.create |
Once all of the initial program windows have been constructed, a call to FXApp#create creates all of the server-side resources for your program. FOX uses a two-step process of first constructing the client-side data for your application and the creating the server-side resources.
main.show(PLACEMENT_SCREEN) |
By default the main window is still not visible. A call to FXMainWindow#show corrects the situation. The PLACEMENT_SCREEN argument for FXMainWindow#show tells FOX to center the main window on the screen when it is displayed.
application.run |
Finally, the call to FXApp#run starts the FOX event loop.