This tutorial provides a detailed walkthrough of creating a “Hello, World!” program in FLTK. We’ll examine each line of code to understand how FLTK applications work.
Title: None (will use default based on executable name)
Constructor variants
Window positioning
Window types
FLTK provides several Fl_Window constructors:
// Width and height onlyFl_Window(int w, int h);// Width, height, and titleFl_Window(int w, int h, const char *title);// Position, size, and titleFl_Window(int x, int y, int w, int h, const char *title = 0);
Example with title:
Fl_Window *window = new Fl_Window(340, 180, "My Application");
Example with specific position:
// Position window at screen coordinates (100, 100)Fl_Window *window = new Fl_Window(100, 100, 340, 180, "My Application");
When position is not specified, FLTK uses the window manager’s default placement:
Sets the label font to bold italic. FLTK provides built-in fonts:
Available fonts
Basic fonts:
FL_HELVETICA // Helvetica regularFL_HELVETICA_BOLD // Helvetica boldFL_HELVETICA_ITALIC // Helvetica italicFL_HELVETICA_BOLD_ITALIC // Helvetica bold italicFL_COURIER // Courier regular (monospace)FL_COURIER_BOLD // Courier boldFL_COURIER_ITALIC // Courier italic FL_COURIER_BOLD_ITALIC // Courier bold italicFL_TIMES // Times regularFL_TIMES_BOLD // Times boldFL_TIMES_ITALIC // Times italicFL_TIMES_BOLD_ITALIC // Times bold italicFL_SYMBOL // Symbol fontFL_SCREEN // Screen/terminal fontFL_SCREEN_BOLD // Screen font boldFL_ZAPF_DINGBATS // Dingbats/symbols
Font modifiers (can be combined with +):
FL_BOLD // Make font boldFL_ITALIC // Make font italic// Exampleslabelfont(FL_HELVETICA + FL_BOLD);labelfont(FL_COURIER + FL_ITALIC);labelfont(FL_TIMES + FL_BOLD + FL_ITALIC);
Adds a shadow effect to the text. Available label types:
FL_NORMAL_LABEL // Standard label (default)FL_SHADOW_LABEL // Label with drop shadowFL_ENGRAVED_LABEL // Engraved appearanceFL_EMBOSSED_LABEL // Embossed/raised appearanceFL_NO_LABEL // No label drawn
Makes the window visible on screen. Passing argc and argv allows FLTK to:
Parse command-line display options (-display, -geometry, etc. on X11)
Set up macOS application bundles correctly
Handle platform-specific startup parameters
show() variants
Window visibility
Window state
// Basic show - no argument parsingwindow->show();// With argument parsing (recommended for main window)window->show(argc, argv);// Show modal window (blocks parent windows)dialog->set_modal();dialog->show();// Show non-modal window (independent window)palette->set_non_modal();palette->show();
// Check if window is visibleif (window->shown()) { // Window is visible}// Hide window (don't destroy)window->hide();// Show againwindow->show();// Iconify (minimize) window window->iconize();
// Make window fullscreenwindow->fullscreen();// Exit fullscreenwindow->fullscreen_off();// Maximize windowwindow->size(Fl::w(), Fl::h());// Set window size limitswindow->size_range(320, 240); // Minimum sizewindow->size_range(320, 240, 1920, 1080); // Min and max
Waits for user events (mouse clicks, keyboard input, window events)
Dispatches events to appropriate widgets and callbacks
Handles window redraws when needed
Continues until all windows are closed
Returns 0 on normal exit
Fl::run() blocks until the last window is closed. Everything that happens in your application (button clicks, drawing, etc.) occurs while Fl::run() is executing.
Event loop alternatives
For advanced applications, you can manually control the event loop:
// Check for events without blockingwhile (window->shown()) { Fl::check(); // Process pending events // Do other work here Fl::wait(0.01); // Wait up to 0.01 seconds for events}
// Wait for specific eventwhile (window->shown()) { if (Fl::wait() == 0) { // Returns 0 if no windows remain break; } // Handle events}