class Fl

This appendix describes all of the fltk::foo() functions. These functions mostly are concerned with event handling and waiting for events. fltk:: should be considered a C++ "namespace". However it is implemented as a class with no instance variables and only static methods, in order to work with older C++ compilers that do not handle namespaces.

FLTK also provides a number of global functions with fltk::xyz names. The majority of these are concerned with drawing or graphics state, and are described in the Drawing appendix. The others are a set of modal popup utility functions for asking questions described in the Utility appendix.

Startup functions

float fltk::version()

Returns the version number of fltk. This can be compared to the value of the fltk::VERSION macro to see if the shared library of fltk your program linked with is up to date.

The FLTK version number is stored in a number of compile-time constants:

void fltk::display(const char *)

Sets the X display to use for all windows. Actually this just sets the environment variable $DISPLAY to the passed string, so this only works before you show() the first window or otherwise open the display, and does nothing useful under WIN32.

int fltk::visual(int)

This call may be useful on multi-visual X servers to change from the default to a more useful color mode. You must call this before you show() any windows. The integer argument is an 'or' of the following: This returns true if the system has the capabilities by default or FLTK suceeded in turing them on. Your program will still work even if this returns false (it just won't look as good). On non-X systems this just returns true or false indicating if the system supports the passed values.

int fltk::gl_visual(int)

This does the same thing as fltk::visual(int) but also requires OpenGL drawing to work. Doing this on X will reduce colormap flashing at the edges of fltk::GlWindows when they are inside regular windows.

See fltk::GlWindow for a list of additional values for the argument.

This function seems to have been renamed fltk::gl_visual(int).

void fltk::own_colormap()

Makes FLTK use its own colormap. This may make FLTK display better and will reduce conflicts with other programs that want lots of colors. However the colors may flash as you move the cursor between windows.

This does nothing if the current visual is not colormapped.

const fltk::ScreenInfo& fltk::screenInfo()

Return a structure of information describing the current state of the screen. Currently this structure contains the following (this may be added to in the future):

Waiting for events

int fltk::wait()

Same as fltk::wait(infinity). Call this repeatedly to "run" your program. You can also check what happened each time after this returns, which is quite useful for managing program state.

int fltk::wait(float time)

Waits until "something happens", or the given time interval passes. It can return much sooner than the time if something happens.

What this really does is call all idle callbacks, all elapsed timeouts, call fltk::flush() to get the screen to update, and then wait some time (zero if there are idle callbacks, the shortest of all pending timeouts, or the given time), for any events from the user or any fltk::add_fd() callbacks. It then handles the events and calls the callbacks and then returns.

The return value is non-zero if there are any visible windows (this may change in future versions of fltk).

The return value is whatever the select() system call returned. This will be negative if there was an error (this will happen on Unix if a signal happens), zero if the timeout occurred, and positive if any events or fd's came in.

On Win32 the return value is zero if nothing happened and time is 0.0. Otherwise 1 is returned.

int fltk::check()

Same as fltk::wait(0). Calling this during a big calculation will keep the screen up to date and the interface responsive:
while (!calculation_done()) {
 calculate();
 fltk::check();
 if (user_hit_abort_button()) break;
}

int fltk::ready()

This is similar to fltk::check() except this does not call fltk::flush() and thus does not draw anything, and does not read any events or call any callbacks. This returns true if fltk::check() would do anything (it will continue to return true until you call fltk::check() or fltk::wait()).

This is useful if your program is in a state where such callbacks are illegal, or because the expense of redrawing the screen is much greater than the expense of your calculation.

while (!calculation_done()) {
 calculate();
 if (fltk::ready()) {
   do_expensive_cleanup();
   fltk::check();
   if (user_hit_abort_button()) break;
 }
}

int fltk::run()

Calls fltk::wait() as long as any windows are not closed. When all the windows are hidden by fltk::Window::hide() (which is normally called by the user closing them with the close box) this will return with zero. A program can also exit by having a callback call exit() or abort().

Most fltk programs will end main() with return fltk::run();.

void fltk::modal(fltk::Widget*, bool grab=false);
fltk::Widget* fltk::modal();
bool fltk::grab();
void fltk::exit_modal();
bool fltk::exit_modal_flag();

First thing: much of the time fltk::Window::exec() will do what you want, so try using that.

This function sets the passed widget as the "modal widget". All user events are directed to it or a child of it, preventing the user from messing with other widgets. The modal widget does not have to be visible or even a child of an fltk::Window for this to work (but if it not visible, fltk::event_x() and fltk::event_y() are meaningless, use fltk::event_x_root() and fltk::event_y_root()).

fltk::exit_modal() sets the fltk::exit_modal_flag(). This may be used by user callbacks to cancel modal state. The flag is also set by the destruction or hiding of the modal widget, and on Windows by other applications taking the focus when grab is on.

The calling code then loops calling fltk::wait() until fltk::exit_modal_flag() is set or you otherwise decide to get out of the modal state. It is the calling code's responsibility to monitor this flag and restore the modal widget to it's previous value when it turns on.

fltk::modal() returns the current modal widget, or null if there isn't one, and fltk::grab() returns the current value of grab (this is always false if the modal widget is null). It is useful to test these in timeouts and file descriptor callbacks in order to block actions that should not happen while the modal window is up. You also need these in order to save and restore the modal state.

grab indicates that the modal widget should get events from anywhere on the screen. This is done by messing with the window system. If fltk::exit_modal() is called in response to an fltk::PUSH event (rather than waiting for the drag or release event) fltk will "repost" the event so that it is handled after modal state is exited. This may also be done for keystrokes in the future. On both X and WIN32 grab will not work unless you have some visible window because the system interface needs a visible window id. On X be careful that your program does not enter an infinite loop while grab() is on, it will lock up your screen!

void fltk::add_timeout(float t, void (*cb)(void*),void*v=0)

Add a one-shot timeout callback. The function will be called by fltk::wait() at t seconds after this function is called. The optional void* argument is passed to the callback.

void fltk::repeat_timeout(float t, void (*cb)(void*),void*v=0)

Inside a timeout callback you can call this to add another timeout. Rather than the time being measured from "now", it is measured from when the system call elapsed that caused this timeout to be called. This will result in far more accurate spacing of the timeout callbacks, it also has slightly less system call overhead. (It will also use all your machine time if your timeout code and fltk's overhead take more than t seconds, as the real timeout will be reduced to zero).

It is undefined what this does if called from outside a timeout callback.

This code will print "TICK" each second on stdout, with a fair degree of accuracy:

void callback(void*) {
  printf("TICK\n");
  fltk::repeat_timeout(1.0,callback);
}

main() {
  fltk::add_timeout(1.0,callback);
  for (;;) fltk::wait();
}

int fltk::has_timeout(void (*cb)(void*), void* = 0)

Returns true if the timeout exists and has not been called yet.

void fltk::remove_timeout(void (*cb)(void*), void* = 0)

Removes a timeout callback. It is harmless to remove a timeout callback that no longer exists.

void fltk::add_check(void (*cb)(void*),void*v=0)

Fltk will call this callback just before it flushes the display and waits for events. This is different than an idle callback because it is only called once, then fltk calls the system and tells it not to return until an event happens.

This can be used by code that wants to monitor the application's state, such as to keep a display up to date. The advantage of using a check callback is that it is called only when no events are pending. If events are coming in quickly, whole blocks of them will be processed before this is called once. This can save significant time and avoid the application falling behind the events.

Sample code:

bool state_changed; // anything that changes the display turns this on

void callback(void*) {
  if (!state_changed) return;
  state_changed = false;
  do_expensive_calculation();
  widget->redraw();
}

main() {
  fltk::add_check(1.0,callback);
  return fltk::run();
}

int fltk::has_check(void (*cb)(void*), void* = 0)

Returns true if the check exists and has not been called yet.

void fltk::remove_check(void (*cb)(void*), void* = 0)

Removes a check callback. It is harmless to remove a check callback that does not exist.

void fltk::add_fd(int fd, void (*cb)(int, void*), void* = 0)
void fltk::add_fd(int fd, int when, void (*cb)(int, void*), void* = 0)

Add file descriptor fd to listen to. When the fd becomes ready for reading fltk::wait() will call the callback and then return. The callback is passed the fd and the arbitrary void* argument.

The second version takes a when bitfield to indicate when the callback should be done. You can or these together to make the callback be called for multiple conditions:

Under UNIX any file descriptor can be monitored (files, devices, pipes, sockets, etc.) Due to limitations in Microsoft Windows, WIN32 applications can only monitor sockets (? and is the when value ignored?)

void fltk::remove_fd(int fd, int when = -1)

Remove all the callbacks (ie for all different when values) for the given file descriptor. It is harmless to call this if there are no callbacks for the file descriptor. If when is given then those bits are removed from each callback for the file descriptor, and the callback removed only if all of the bits turn off.

fltk::add_idle(void (*cb)(void*), void*)

Adds a callback function that is called every time by fltk::wait() and also makes it act as though the timeout is zero (this makes fltk::wait() return immediately, so if it is in a loop it is called repeatedly, and thus the idle fucntion is called repeatedly). The idle function can be used to get background processing done.

You can have multiple idle callbacks. To remove an idle callback use fltk::remove_idle().

fltk::wait() and fltk::check() call idle callbacks, but fltk::ready() does not.

The idle callback can call any FLTK functions, including fltk::wait(), fltk::check(), and fltk::ready(). Fltk will not recursively call the idle callback.

int fltk::has_idle(void (*cb)(void*), void* = 0)

Returns true if the specified idle callback is currently installed.

void fltk::remove_idle(void (*cb)(void*), void* = 0)

Removes the specified idle callback, if it is installed.

Redrawing

int fltk::damage()

True if any fltk::Widget::redraw() calls have been done since the last flush().

void fltk::redraw()

Redraws all widgets. This is a good idea if you have made global changes to the styles.

void fltk::flush()

Causes all the windows that need it to be redrawn and graphics forced out through the pipes. This is what wait() does before looking for events.

fltk::Window *fltk::first_window()

Returns the id of some visible() window. If there is more than one, the last one to receive an event is returned. This is useful for setting the fltk::Window::child_of() for dialog boxes that are used in multiple places in your program, and is used by fltk::Window::exec() if no other parent is specified.

void fltk::first_window(fltk::Window*)

If this window is visible, this removes it from wherever it is in the list and inserts it at the top, as though it received an event. This can be used to change the parent of dialog boxes run by fltk::Window::exec() or fltk::ask().

fltk::Window *fltk::next_window(fltk::Window *)

Returns the next visible() top-level window, returns NULL after the last one. You can use this and first_window() to iterate through all the visible windows.

Events

int fltk::event()

Returns the most recent event handled, such as fltk::PUSH or fltk::KEY. This is useful so callbacks can find out why they were called.

int fltk::event_x()
int fltk::event_y()

Returns the mouse position of the event relative to the fltk::Window it was passed to.

int fltk::event_dx()
int fltk::event_dy()

For fltk::MOUSEWHEEL events this is how many clicks the user moved in the x and y directions (currently dx is always zero).

int fltk::event_x_root()
int fltk::event_y_root()

Returns the mouse position on the screen of the event. To find the absolute position of an fltk::Window on the screen, use the difference between event_x_root(),event_y_root() and event_x(),event_y().

int fltk::event_clicks()
void fltk::event_clicks(int)

Returns the number of times the last mouse button or keyboard key was pushed while fltk::event_is_click() was true.

For a normal fltk::PUSH this is zero, if the user "double-clicks" this is one, and it is N-1 for each subsequent click. This is also used to see if the keyboard is repeating, if the most recent fltk::KEY was caused by a repeating key this is non-zero, and is N-1 for N repeats of the key.

Setting this value with fltk::event_clicks(n) can be used to make callbacks think things were (or were not) double-clicked.

int fltk::event_is_click()
void fltk::event_is_click(0)

This is true if the time and mouse movement since the last fltk::PUSH or fltk::KEY is short enough that the user is intending to "click". After enough time or after enough mouse movement this turns off.

The main use of this is to decide whether to increment fltk::event_clicks() when the user clicks the mouse. But you can also test this on a fltk::RELEASE or fltk::KEYUP event to decide if the user clicked quickly, versus holding the mouse or key down.

You can set this to zero with fltk::event_is_click(0), this can be used to prevent the next mouse click from being considered a double click. It is not possible to set this true because the saved time and position are inaccessible.

int fltk::event_button()

Returns which mouse button was last pushed or released. You can use numerical constants or these values:

ulong fltk::event_state()
bool fltk::event_state(ulong)

This is a bitfield of what shift states were on and what mouse buttons were held down during the most recent event. The second version returns true if any of the passed bits are turned on. The legal bits are: X servers do not agree on shift states, so fltk::NUM_LOCK, fltk::WIN, and fltk::SCROLL_LOCK may not work. The values were selected to match the XFree86 server on Linux. In addition there is a bug in the way X works so that the shift state is not correctly reported until the first event after the shift key is pressed or released.

int fltk::event_key()

Returns which key on the keyboard was last pushed. fltk::FOCUS events produced by the system, and mouse button events, set this to values that cannot be confused with keys, to allow a widget to determine if the focus is being changed due to a keystroke and what keystroke that was.

Keys are identified by the unshifted values. FLTK defines a set of symbols that should work on most modern machines for every key on the keyboard:

bool fltk::event_key_state(int key)

Returns true if the given key was held down (or pressed) during the last event. This is constant until the next event is read from the server. The possible values for the key are listed above.

On Win32 fltk::event_key_state(fltk::KP_Enter) does not work.

char *fltk::event_text()

Returns the ASCII text (in the future this may be UTF-8) produced by the last fltk::KEY or fltk::PASTE or possibly other event. A zero-length string is returned for any keyboard function keys that do not produce text. This pointer points at a static buffer and is only valid until the next event is processed.

Under X this is the result of calling XLookupString().

char *fltk::event_length()

Returns the length of the text in fltk::event_text(). There will always be a nul at this position in the text. However there may be a nul before that if the keystroke translates to a nul character or you paste a nul character.

int fltk::event_inside(int x, int y, int w, int h)

Returns non-zero if the current fltk::event_x() and fltk::event_y() put it inside the passed box. You should always call this rather than doing your own comparison so you are consistent about edge effects.

int fltk::test_shortcut(int shortcut)

Test the current event, which must be an fltk::KEY or fltk::SHORTCUT, against a shortcut value (described in fltk::Button). Returns non-zero if there is a match. Not to be confused with fltk::Widget::test_shortcut().

const char* fltk::key_name(int shortcut)

Unparse a key name (as returned by fltk::event_key()) or a shortcut value (as used by fltk::Button or Fl_Menu_Item) into a human-readable string like "Alt+N". If the shortcut is zero an empty string is returned. The return value points at a static buffer that is overwritten with each call.

int fltk::compose(int& del)

Use of this function is very simple. Any text editing widget should call this for each fltk::KEY event.

If true is returned, then it has modified the fltk::event_text() and fltk::event_length() to a set of bytes to insert (it may be of zero length!). It will also set the del parameter to the number of bytes to the left of the cursor to delete, this is used to delete the results of the previous call to fltk::compose().

If false is returned, the keys should be treated as function keys. You could insert the text anyways, if you don't know what else to do, del is set to zero and the fltk::event_text() and fltk::event_length() are left unchanged, length is zero for any function keys.

Though the current implementation returns immediately, future versions may take quite awhile, as they may pop up a window or do other user-interface things to allow characters to be selected.

int fltk::compose_reset()

If the user moves the cursor, be sure to call fltk::compose_reset(). The next call to fltk::compose() will start out in an initial state. In particular it will not set "del" to non-zero. This call is very fast so it is ok to call it many times and in many places.

int fltk::get_key_state(int)

Returns true if the given key is held down now. Under X this requires a round-trip to the server and is much slower than fltk::event_key_state(int).

On Win32 fltk::get_key_state(fltk::KP_Enter) does not work.

void fltk::get_mouse(int &x, int &y)

Return where the mouse is on the screen by doing a round-trip query to the server. You should use fltk::event_x_root() and fltk::event_y_root() if possible, but this is necessary if you are not sure if a mouse event has been processed recently (such as to position your first window). If the display is not open, this will open it.

void fltk::add_handler(int (*f)(int))

Install a function to parse unrecognized events. If FLTK cannot figure out what to do with an event, it calls each of these functions (most recent first) until one of them returns non-zero. If none of them returns non zero then the event is ignored.

Currently there are only two uses for this:

1. If there is a keystroke that no widgets are interested in, this is called with fltk::SHORTCUT. You can use this to implement global shortcut keys.

2. On X, if FLTK does not recognize an X event or the window id the event is sent to, this is called with zero. You can then use system specific code to access the event data and figure out what to do. (this is not done on Win32 due to the enormous number of bogus events sent, it was much to slow to search the handler list for every one).

Ignore any other values this is called with. We have not figured out what the rules for these are yet.

fltk::Widget* fltk::belowmouse() const

Get the widget that is below the mouse. This is the last widget to respond to an fltk::ENTER event as long as the mouse is still pointing at it. This is for highlighting buttons and bringing up tooltips. It is not used to send fltk::PUSH or fltk::MOVE directly, for several obscure reasons, but those events typically go to this widget.

void fltk::belowmouse(fltk::Widget*)

Change the fltk::belowmouse() widget, the previous one and all parents (that don't contain the new widget) are sent fltk::LEAVE events. Changing this does not send fltk::ENTER to this or any widget, because sending fltk::ENTER is supposed to test if the widget wants the mouse (by it returning non-zero from handle()).

fltk::Widget* fltk::pushed() const;

Get the widget that is being pushed. fltk::DRAG or fltk::RELEASE (and any more fltk::PUSH) events will be sent to this widget. This is null if no mouse button is being held down, or if no widget responded to the fltk::PUSH event.

void fltk::pushed(fltk::Widget*)

Change the fltk::pushed() widget, the previous one and all parents (that don't contain the new widget) are sent fltk::RELEASE events. Changing this does not send fltk::PUSH to this or any widget, because sending fltk::PUSH is supposed to test if the widget wants the mouse (by it returning non-zero from handle()).

fltk::Widget* fltk::focus() const;

Returns the widgets that will receive fltk::KEY events. This is NULL if the application does not have focus now, or if no widgets accepted focus.

void fltk::focus(fltk::Widget *)

Change fltk::focus() to the given widget, the previous widget and all parents (that don't contain the new widget) are sent fltk::UNFOCUS events, the new widget and all parents that don't contain the old widget are sent fltk::FOCUS events. fltk::focus() is set whether or not the applicaton has the focus or if the widgets accept the focus. You may want to use fltk::Widget::take_focus() instead, it will test first.

Copy, Paste, and Drag & Drop

void fltk::copy(const char *stuff, int len, bool clipboard=false)

Change the current selection. The block of text is copied to an internal buffer by FLTK (be careful if doing this in response to an fltk::PASTE as this may be the same buffer returned by event_text()).

The block of text may be retrieved (from this program or whatever program last set it) with fltk::paste().

There are actually two buffers. If clipboard is true then the text goes into the user-visible selection that is moved around with cut/copy/paste commands (on X this is the CLIPBOARD selection). If clipboard is false then the text goes into a less-visible buffer used for temporarily selecting text with the mouse and for drag & drop (on X this is the XA_PRIMARY selection).

Copying the buffer every time the selection is changed is obviously wasteful, especially for large selections. An interface will probably be added in a future version to allow the selection to be made by a callback function. The current interface will be emulated on top of this.

void fltk::paste(fltk::Widget *receiver, bool clipboard = false)

This is what a widget does when a "paste" command (like Ctrl+V or the middle mouse click) is done to it. Cause an fltk::PASTE event to be sent to the receiver with the contents of the current selection in the fltk::event_text(). The selection can be set by fltk::copy().

There are actually two buffers. If clipboard is true then the text is from the user-visible selection that is moved around with cut/copy/paste commands (on X this is the CLIPBOARD selection). If clipboard is false then the text goes into a less-visible buffer used for temporarily selecting text with the mouse and for drag & drop (on X this is the XA_PRIMARY selection).

The reciever should be prepared to be called directly by this, or for it to happen later, or possibly not at all. This allows the window system to take as long as necessary to retrieve the paste buffer (or even to screw up completely) without complex and error-prone synchronization code most toolkits require.

bool fltk::dnd()

Drag and drop the data set by the most recent fltk::copy() (with the clipboard argument false). Returns true if the data was dropped on something that accepted it.

By default only blocks of text are dragged. You can use system-specific variables to change the type of data.

Multithreading

void fltk::lock();

Blocks the current thread until it can safely access FLTK widgets and data. Child threads should call this method prior to updating any widgets or accessing data. The main thread must call fltk::lock() to initialize the threading support in FLTK before calling fltk::wait() or fltk::run().

Child threads must call fltk::unlock() when they are done accessing FLTK. They may want to call fltk::awake() first if the display needs to change.

This is a "recursive lock". If you call fltk::lock() more than once, the subsequent calls return immediately. But you must call fltk::unlock() the same number of times as you called fltk::lock() before the lock is released.

When the wait() method is waiting for input or timeouts, child threads are given access to FLTK. Similarly, when the main thread receives events and needs to do processing, it will wait until all child threads have called unlock() before processing the events and doing callbacks.

See the file <fltk/Threads.h> for a simple portable recursive lock object you can use in your own code for locking other objects. However there is no requirement that you use this, you can use pthreads or any other library that is compatable with your system.

void fltk::unlock();

Releases the lock that was set using the fltk::lock() method. Child threads should call this method as soon as they are finished accessing FLTK. If some other thread is waiting for fltk::lock() to return, it will get control.

void fltk::awake(void* message = 0);

Make the main thread (the one that is calling fltk::wait()) wake up. The main purpose of this is to get the main thread to redraw the screen, but it will also cause fltk::wait() to return so the program's code can do something.

Due to obscure race problems you must call this after you call fltk::unlock(). If you call this while things are locked both the X and Win32 implementations can deadlock.

The message argument can be retrieved by the other thread using fltk::thread_message().

void* thread_message();

Returns an argument sent to an fltk::awake() call, or returns null if none. The current implementation only has a one-entry queue and only returns the most recent value!

Errors

void (*fltk::warning)(const char *, ...)
void (*fltk::error)(const char *, ...)
void (*fltk::fatal)(const char *, ...)

FLTK will call these to print messages when unexpected conditions occur.

fltk::warning means that there was a recoverable problem, the display may be messed up but the user can probably keep working. (all X protocol errors call this).

fltk::error means there is a recoverable error, but the display is so messed up it is unlikely the user can continue (very little calls this now).

fltk::fatal must not return, as FLTK is in an unusable state, however your version may be able to use longjmp or an exception to continue, as long as it does not call FLTK again.

The default versions on Unix print messages to stderr, while on Windows they use MessageBox(). fltk::error and fltk::fatal call abort(). You can override the behavior by setting the function pointers to your own routines.