Writing Your First FLTK Program

All programs must include the file <FL/Fl.H>. In addition the program must include a header file for each FLTK class it uses. Example 8-1 shows a simple "Hello, World!" program that uses FLTK to display the window.

Example 8-1. hello.cxx

#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(20,40,260,100,"Hello, World!");
  box->box(FL_UP_BOX);
  box->labelsize(36);
  box->labelfont(FL_BOLD+FL_ITALIC);
  box->labeltype(FL_SHADOW_LABEL);
  window->end();
  window->show(argc, argv);
  return Fl::run();
}
        

After including the required header files, the program then creates a window:

Fl_Window *window = new Fl_Window(300,180);
      

and a box with the "Hello, World!" string in it:

Fl_Box *box = new Fl_Box(20,40,260,100,"Hello, World!");
      

Next, we set the type of box and the size, font, and style of the label:

box->box(FL_UP_BOX);
box->labelsize(36);
box->labelfont(FL_BOLD+FL_ITALIC);
box->labeltype(FL_SHADOW_LABEL);
      

Finally, we show the window and enter the FLTK event loop:

window->end();
window->show(argc, argv);
return Fl::run();
      

The resulting program will display the window below. You can quit the program by closing the window or pressing the ESCape key.

hello.C

Creating the Widgets

The widgets are created using the C++ new operator. For most widgets the arguments to the constructor are:

Fl_Widget(x, y, width, height)
        

The x and y parameters determine where the widget or window is placed on the screen. In FLTK the top left corner of the window or screen is the origin (i.e. x = 0, y = 0) and the units are in pixels.

The width and height parameters determine the size of the widget or window in pixels. The maximum widget size is typically governed by the underlying window system or hardware.

label is a pointer to a character string to label the widget with or NULL. If not specified the label defaults to NULL. The label string must be in static storage (such as a string constant) because FLTK does not make a copy of it (it just uses the pointer).

Get/Set Methods

box->box(FL_UP_BOX) sets the type of box the Fl_Box draws, changing it from the default of FL_NO_BOX, which means that no box is drawn. In our "Hello, World!" example we use FL_UP_BOX, which means that a raised button border will be drawn around the widget. You can learn more about boxtypes in Chapter 9.

You could examine the boxtype in by doing box->box(). Fltk uses method name overloading to make short names for get/set methods. A "set" method is always of the form "void name(type)", and a "get" method is always of the form "type name() const".

Almost all of these set/get pairs are very fast and short inline functions and thus very efficient. However, the "set" methods do not call redraw(), you have to call it yourself. This greatly reduces code size and execution time. The only common exception is value(), this does redraw() if necessary.

Labels

All widgets support labels. In the case of window widgets, the label is used for the label in the title bar. Our example program calls the labelfont, labelsize, and labeltype methods.

The labelfont method sets the typeface and style that is used for the label, which for this example we are using FL_BOLD and FL_ITALIC. You can also specify typefaces directly.

The labelsize method sets the height of the font in pixels.

The labeltype method sets the type of label. FLTK supports normal, embossed, shadowed, symbol, and image labels.

A complete list of all label options can be found in Chapter 9.

Showing the Window

The show() method shows the widget or window. For windows you can also provide the command-line arguments to allow users to customize the appearance, size, and position of your windows.

The Main Event Loop

FLTK provides the Fl:run() method to enter a standard event processing loop. This is equivalent to the following code:

while (Fl::wait());
        

Fl::run() does not return until all of the windows under FLTK control are closed (either by the user or your program).