In this second part to a two-part series on creating reports on the desktop with Ruby-on-Rails, you'll learn how to create GUIs with Ruby and take a look at a sample report. It is excerpted from chapter four of the book Practical Reporting with Ruby and Rails, written by David Berube (Apress; ISBN: 1590599330).
GUIs and More for Desktop Reports - Using FXRuby (Page 2 of 4 )
Install FXRuby as follows:
gem install fxruby
Additionally, you'll need an X11 server running under Linux or Mac OS X. Most Linux distributions come with an X11 server, and you can install it from the distribution CD if you don't have it currently loaded. Mac OS X is much the same--you can install an X11 server from the CD included with your computer. You'll need to explicitly launch the server under either operating system. To do so under Linux, use the startx command . On an OS X system, click the X11 icon.
Finally, you'll also need the FOX library installed. This library is included with the FXRuby gem under Windows, but you can install it fairly easily on other operating systems via your favorite software packaging method: MacPorts (port install rb-fxruby) or Apt (apt-get install fox fox-devel). You can also download and install it from http://www.foxtoolkit.org/.
After you've set up the prerequisites, you can try the simple example in FXRuby shown in Listing 4-2.
Listing 4-2.Simple FXRuby Example (simple_fx_ruby_example.rb)
require 'fox16'
include Fox
myApp = FXApp.new
mainWindow=FXMainWindow.new(myApp, "Simple FXRuby Control Demo", :padding =>10, :vSpacing=>10)
my_first_button= FXButton.new(mainWindow, 'Example Button Control') my_first_button.connect(SEL_COMMAND) do my_first_button.text="In a real-life situation, this would do something." end
FXTextField.new(mainWindow, 30).text = 'Example Text Control'
FXRadioButton.new(mainWindow, "Example Radio Control") FXCheckButton.new(mainWindow, "Example Check Control")
myApp.create
mainWindow.show( PLACEMENT_SCREEN )
myApp.run
You can run the example as follows :
ruby simple_fxruby_example.rb
Your application will look like Figure 4-2.
Figure 4-2.A simple FXRuby example
Let's take a look at the simple FXRuby demo in Listing 4-2.
Dissecting the Code
Listing 4-2 begins by creating an FXApp object, which represents your application, and then an FXMainWindow object, which holds all of the controls. The FXApp object handles application-wide tasks, like updating the mouse cursor and so forth.
Here, you set a couple properties. The padding property is set to 10, which gives 10 pixels of space on each side. The vSpacing property is also set to 10, for 10 pixels between each control. Taken together, these two properties make the controls evenly spaced out so that the application is visually attractive. By default, controls are arranged top to bottom; however, you can specify other arrangements, as you'll see in the next section.
After your FXApp and FXMainWindow objects are created, you can add your first control :
my_first_button= FXButton.new(mainWindow, 'Example Button Control') my_first_button.connect(SEL_COMMAND) do puts "You've clicked the button!" end
This is an FXButton control, which is designed to allow users to perform a specific action. The first line creates the control with the text "Example Button Control," and the following lines specify an action to take when the user clicks the button.
Next, you create a few more interface widgets:
FXTextField.new(mainWindow, 30).text = 'Example Text Control'
FXRadioButton.new(mainWindow, "Example Radio Control") FXCheckButton.new(mainWindow, "Example Check Control")
The FXTextField object lets the user enter text. The text can be retrieved or preset using the text property, as in this example. You then create an FXRadioButton object, which lets the user select just one button from a group (of course, typically you would have more than one radio button). The last line creates an FXCheckButton control, which lets the user make an on/off choice.
Finally, having specified the elements for your interface, you then need to perform some actions. First, you call the create method on your application object. This calls th e create method on all of your FXRuby objects, which uses the FOX GUI toolkit to actually create the objects and prepare them for display:
myApp.create
mainWindow.show( PLACEMENT_SCREEN )
The second line calls the show method, which, logically enough, shows the main window. The PLACEMENT_SCREEN constant specifies that the window should be centered on the screen.
As you can see, it's reasonably easy to create different types of controls: command buttons, text fields, radio (often called "option") buttons, check boxes, and so forth. (See the FXRuby documentation at http://www.fxruby.org/ for information about the other controls available and how they work.) Now let's apply the same basic techniques to a slightly more complicated example.