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->end();
  window->show(argc, argv);
  return Fl::run();
}

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

The program then creates a window and then creates the widgets 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 widgets 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 widget 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 constructor for widgets adds them as children of the "current group" (usually a window). window->end() stops adding them to this window. For more control over the construction of objects, you can end() the window immediately, and then add the objects with window->add(box). You can also do window->begin() to switch what window new objects are added to.

window->show() finally puts the window on the screen. It is not until this point that the X server is opened. Fltk 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::run() makes Fltk enter a loop to update the screen and respond to events. By default when the user closes the last window fltk exits by calling exit(0). run() does not actually return, it is declared to return an int so you can end your main() function with "return Fl::run()" and outwit the stupid compiler made by a certain very large software company.

The following command compiles this program, assuming the fltk 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]