c++ -I/usr/local/include ...(In Visual C++ this can be done by selecting "Settings" from the "Project" menu and then changing the "Preprocessor" settings under the "C/C++" tab)
Similarly, when linking your application you will need to tell the compiler to use the FLTK library and where to find it:
c++ ... -L/usr/local/lib -lfltk -lXext -lX11 -lm(In Visual C++ you need to add the FLTK library to the "Link" settings).
#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->label_font(FL_HELVETICA_BOLD_ITALIC); box->label_size(36); box->label_type(FL_SHADOW_LABEL); 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.
#include <FL/Fl_xyz.H>Notice that the leading "FL" and the ".H" are capitalized. (perhaps this will be changed for 2.0?)
All programs must include the file <FL/Fl.H>. In addition the program must include a header file for each FLTK class it uses. This sample program uses Fl_Window and Fl_Box.
Programs that call FLTK drawing functions must include <FL/fl_draw.H>
After including the required header files, the program then creates a window:
Fl_Window *window = new Fl_Window(300,180);It then creates a box with the "Hello, World!" string in it, this new widget is made a "child" of the window we just created:
Fl_Box *box = new Fl_Box(20,40,260,100,"Hello, World!");
For most widgets the arguments to the constructor are:
Fl_Widget(x, y, width, height, label)
x and y are the location of the upper-left corner of the widget, measured in pixels. For windows this is measured from the upper-left corner of the screen, for widgets it is measured from the upper-left corner of the window.
width and height are the size of the widget.
label is a pointer to a character string to label the widget with or NULL. If not given it defaults to NULL. The string is not copied, FLTK assummes it resides in static storage. This is true of almost all interfaces in FLTK that take string constants, and greatly speeds up FLTK. (see Fl_Widget::copy_label() for a method that has more traditional behavior).
box->box(FL_UP_BOX); box->label_font(FL_HELVETICA_BOLD_ITALIC); box->label_size(36); box->label_type(FL_SHADOW_LABEL);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 see the available boxtypes in Chapter 3)
You can 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.
window->end();
To simplify the code you have to write, the constructor for all widgets automatically adds them as children of the most recent Fl_Group or Fl_Window that was created. Though convienent, this is also a big source of errors in FLTK programming. The method Fl_Group::end() restores the "current group" to the parent of itself, in this case it is set to null because the window has no parent. (you may also find it useful to call Fl_Group::current(0) to turn this off completely.
window->show(argc, argv);
For the first Fl_Window you can provide the command-line arguments, this runs a rather simple default argument parser that lets the user customize the appearance, size, and position of your windows (you are not required to use this argument parser, and just calling show() with no arguments does not do this).
(On some systems the window does not actually appear until Fl::run() is called, which flushes the cached instructions to the window server).
return Fl::run();FLTK was designed to not take over the main event loop. Instead a program can call Fl::wait(), which will pause until "something happens" and then return. A program can call Fl::wait() repeatedly or mix it with other calculations.
For convienence with compilers that produce warnings when there are infinite loops in your code, FLTK provides the Fl:run() method. It is equivalent to:
for (;;) Fl::wait();
When the user attempts to close the window, the callback for the window is called. If you don't change it, the callback will remove the window from the screen and if there are no windows left on the screen it will call exit(0).
You can replace this callback with your own code, so you can prevent the window from closing, or pop up a confirmation, or create another window, or close it and keep running with no windows (useful if you used Fl::add_fd()), or you can make closing the "main" window exit even if other windows are open. Fl::run() does not return until all of the windows under FLTK control are closed (either by the user or your program).