hello.C

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>

int main(int argc, char **argv) {
  Fl_Window *window = new Fl_Window(300,180);
  Fl_Box *box = new Fl_Box(FL_UP_BOX,20,40,260,100,"Hello, World!");
  box->labelsize(36);
  box->labelfont(FL_BOLD+FL_ITALIC);
  box->labeltype(FL_SHADOW_LABEL);
  window->add(box);
  window->show(argc, argv);
  for (;;) Fl::wait();
}

All programs must include the file <FL/Fl.H>. In addition the program must include a header file for each Fl class it uses, here <FL/Fl_Window.H> and <FL/Fl_Box.H>.

The program then creates a window and then creates the objects inside the window. Here a single Fl_Box is created. The arguments to the constructor are a value for the box() property (most constructors do not have this), values for x(), y(), w(), h() to define the position and size of the box, and a value for label() to define the text printed in the box.

All the objects have several attributes and there is a method for setting and getting the current value of each of them. box->labelsize(36) sets the labelsize() to 36. You could get the value with box->labelsize(). Often you have to set many properties, so you will be relieved to know that almost all of these methods are trivial inline functions.

labelfont() is set to a symbolic value which is compiled into a constant integer, 3 in this case. All properties that cannot be described by a single small number use a 1-byte index into a table. This makes the object smaller, allows the actual definition of the property to be deferred until first use, and you can redefine existing entries to make global style changes.

labeltype(FL_SHADOW_LABEL) also stores a 1-byte symbolic value, in this case indicating a procedure to draw drop shadows under the letters should be called to draw the label.

The box will not be visible unless it is made a child of the window. This is done with window->add(box). You can also do window->begin() before creating the box, this will cause the object constructor to automatically add to the window (however g++ and possibly other compilers may complain that the box is unused).

window->show() finally puts the window on the screen. It is not until this point that the X server is opened. FL provides some optional and rather simple command-line parsing if you call show(argv,argc). If you don't want this, just call show() with no arguments, and the unused argument code is not linked into your program, making it smaller!

Fl::wait() waits for an event and then processes all pending ones. Calling it repeatedly will let your program run. It is often useful to test and respond to state information after each time wait() returns. In this case though we don't test anything. Eventually the user will get tired of looking at the window and will close it. By default when the user closes the last window Fl exits by calling exit(0).

The following command compiles this program, assuming the FL library has been put in /usr/local/lib and the header files in /usr/local/include/FL:

CC hello.C -lFl -lX11 -o hello

[Next example]
[back to contents]