This appendix describes all of the Fl::xyz functions. These
functions mostly are concerned with event handling and waiting for
events. Fl:: should be considered a namespace, although it
is defined as a C++ class in the header file to work with older C++
compilers.
FLTK also provides a number of global functions with
fl_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.
Also see the description of all the classes listed in the Classes appendix.
Add file descriptor fd to listen to. When the fd
becomes ready for reading Fl::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, with the bits
FL_READ, FL_WRITE, and FL_EXCEPT defined,
to indicate when the callback should be done.
There can only be one callback of each type for a file descriptor.
Fl::remove_fd() gets rid of all the callbacks for a given
file descriptor.
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.
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. Events that cause
this to be called are:
- FL_SHORTCUT events that are not recognized by any widget.
This lets you provide global shortcut keys.
- System events that FLTK does not recognize. See fl_xevent.
- Some other events when the widget FLTK selected returns
zero from its handle() method. Exactly which ones may change
in future versions, however.
Adds a callback function that is called every time by
Fl::wait() and also makes it act as though the timeout is
zero (this makes Fl::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 Fl::remove_idle().
Fl::wait() and Fl::check() call idle callbacks,
but Fl::ready() does not.
The idle callback can call any FLTK functions, including
Fl::wait(), Fl::check(), and Fl::ready().
Fltk will not recursively call the idle callback.
Add a one-shot timeout callback. The function will be called by
Fl::wait() at t seconds after this function is called.
The optional void* argument is passed to the callback.
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");
Fl::repeat_timeout(1.0,callback);
}
main() {
Fl::add_timeout(1.0,callback);
for (;;) Fl::wait();
}
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() {
Fl::add_check(1.0,callback);
return Fl::run();
}
Consume a single switch from argv, starting at word i.
Returns the number of words eaten (1 or 2, or 0 if it is not
recognized) and adds the same value to i. You can use this
function if you prefer to control the incrementing through the
arguments yourself.
FLTK provides an entirely optional command-line switch parser.
You don't have to call it if you don't like them! Everything it can do
can be done with other calls to FLTK.
To use the switch parser, call Fl::args(...) near the
start of your program. This does not open the display, instead
switches that need the display open are stashed into static variables.
Then you must display your first window by calling window->show(argc,argv),
which will do anything stored in the static variables.
callback lets you define your own switches. It is called
with the same argc and argv, and with i the
index of each word. The callback should return zero if the switch is
unrecognized, and not change i. It should return non-zero if
the switch is recognized, and add at least 1 to i (it can add
more to consume words after the switch). This function is called
before any other tests, so you can override any FLTK
switch (this is why fltk can use very short switches instead of
the long ones all other toolkits force you to use).
On return i is set to the index of the first non-switch.
This is either:
- The first word that does not start with '-'.
- The word '-' (used by many programs to name stdin as a file)
- The first unrecognized switch (return value is 0).
- argc
The return value is i unless an unrecognized switch is found,
in which case it is zero. If your program takes no arguments other
than switches you should produce an error if the return value is less
than argc.
All switches except -bg2 may be abbreviated one letter and case is
ignored:
- -display host:n.n The X display to use (ignored under
WIN32).
- -geometry WxH+X+Y The window position and size will be
modified according the the standard X geometry string.
- -name string Fl_Window::xclass(string) will be done to
the window, possibly changing its icon.
- -title string Fl_Window::label(string) will be done to
the window, changing both its title and the icontitle.
- -iconic Fl_Window::iconize() will be done to the window.
- -bg color XParseColor is used to lookup the passed color
and then Fl::background() is done. Under WIN32 only color names of
the form "#xxxxxx" are understood.
The second form of Fl::args() is useful if your program does
not have command line switches of its own. It parses all the switches,
and if any are not recognized it calls Fl::abort(Fl::help).
Get or set the widget that is below the mouse. This is for
highlighting buttons. It is not used to send FL_PUSH or
FL_MOVE directly, for several obscure reasons, but those events
typically go to this widget. This is also the first widget tried for
FL_SHORTCUT events.
If you change the belowmouse widget, the previous one and all
parents (that don't contain the new widget) are sent FL_LEAVE
events. Changing this does not send FL_ENTER to this
or any widget, because sending FL_ENTER is supposed to test
if the widget wants the mouse (by it returning non-zero from
handle()).
Same as Fl::wait(0). Calling this during a big calculation
will keep the screen up to date and the interface responsive:
while (!calculation_done()) {
calculate();
Fl::check();
if (user_hit_abort_button()) break;
}
If true then flush() will do something.
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.
Returns the most recent event handled, such as FL_PUSH or
FL_KEYBOARD. This is useful so callbacks can find out why
they were called.
Returns the mouse position of the event relative to the Fl_Window
it was passed to.
Returns the mouse position on the screen of the event. To find the
absolute position of an Fl_Window on the screen, use the
difference between event_x_root(),event_y_root() and
event_x(),event_y().
Returns which mouse button was pressed. This returns garbage if the
most recent event was not a FL_PUSH or FL_RELEASE
event.
The first form returns non-zero if the most recent FL_PUSH or
FL_KEYBOARD was a "double click". Returns N-1 for N clicks. A
double click is counted if the same button is pressed again while
event_is_click() is true.
The second form directly sets the number returned by
Fl::event_clicks(). This can be used to set it to zero so that
later code does not think an item was double-clicked.
Returns non-zero if the current event_x and 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.
The first form returns non-zero if the mouse has not moved far enough
and not enough time has passed since the last FL_PUSH or
FL_KEYBOARD event for it to be considered a "drag" rather than a
"click". You can test this on FL_DRAG, FL_RELEASE,
and FL_MOVE events. The second form clears the value returned
by Fl::event_is_click(). Useful to prevent the next
click from being counted as a double-click or to make a popup menu
pick an item with a single click. Don't pass non-zero to this.
Fl::event_key() returns which key on the keyboard was last
pushed. It returns zero if the last event was not a key press or release.
Fl::event_key(int) 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.
Fl::get_key(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 Fl::event_key(int).
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:
- All keys on the main keyboard producing a printable ASCII
character use the value of that ASCII character (as though shift,
ctrl, and caps lock were not on). The space bar is 32.
- All keys on the numeric keypad producing a printable ASCII
character use the value of that ASCII character plus FL_KP.
The highest possible value is FL_KP_Last so you can
range-check to see if something is on the keypad.
- All numbered function keys use the number on the function key plus
FL_F. The highest possible number is FL_F_Last, so you
can range-check a value.
- Buttons on the mouse are considered keys, and use
FL_Button+n, where n is the button number (the left button is
1).
- All other keys on a US 101-keyboard have a symbol. Be
careful not to confuse these with the very similar, but all-caps,
symbols used by Fl::event_state()
FL_Escape
FL_BackSpace
FL_Tab
FL_Enter
FL_Print
FL_Scroll_Lock
FL_Pause
FL_Insert
FL_Home
FL_Page_Up
FL_Delete
FL_End
FL_Page_Down
FL_Left
FL_Up
FL_Right
FL_Down
FL_Shift_L
FL_Shift_R
FL_Control_L
FL_Control_R
FL_Caps_Lock
FL_Alt_L
FL_Alt_R
FL_Meta_L
FL_Meta_R
FL_Menu
FL_Num_Lock
FL_KP_Enter
- On X systems any unrecognized keys are reported as their X keysym
value.
On X Fl::get_key(FL_Button+n) does not work.
On WIN32 Fl::get_key(FL_KP_Enter) and
Fl::event_key(FL_KP_Enter) do not work.
Returns the length of the text in Fl::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.
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 non-zero if any of the passed bits are turned on. The legal
bits are:
- FL_SHIFT
- FL_CAPS_LOCK
- FL_CTRL
- FL_ALT
- FL_NUM_LOCK
- FL_META
- FL_SCROLL_LOCK
- FL_BUTTON1
- FL_BUTTON2
- FL_BUTTON3
X servers do not agree on shift states, and FL_NUM_LOCK, FL_META, and
FL_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.
char *Fl::event_text()
Returns the ASCII text (in the future this may be UTF-8) produced by
the last FL_KEYBOARD or FL_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().
int Fl::compose(int& del)
Use of this function is very simple. Any text editing widget should
call this for each FL_KEYBOARD event.
If true is returned, then it has modified the Fl::event_text() and Fl::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 Fl::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 Fl::event_text() and Fl::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 Fl::compose_reset()
If the user moves the cursor, be sure to call Fl::compose_reset().
The next call to Fl::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.
Returns the first visible() top-level window. This is the
modal() window if it exists, if there are no
modal windows it is the most recent window to get an event. Returns
NULL if no windows are visible.
Since first_window() is used by default as
the "parent" of modal windows, changing it is useful. This removes
window from wherever it is in the list and inserts it at the
top, as though it received an event. Code that then shows a modal
window or calls Fl_Window::exec()
will make this window the "parent".
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.
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.
Returns the widgets that will receive FL_KEYBOARD
events. This is NULL if the application does not have focus now, or if
no widgets accepted focus.
void Fl::focus(Fl_Widget *)
Change Fl::focus() to the given widget, the previous widget and all
parents (that don't contain the new widget) are sent FL_UNFOCUS
events, the new widget and all parents that don't contain the old
widget are sent FL_FOCUS events. focus() is set whether or
not the applicaton has the focus or if the widgets accept the
focus. You may want to use Fl_Widget::take_focus() instead, it
will test first.
Return where the mouse is on the screen by doing a round-trip query to
the server. You should use Fl::event_x_root() and
Fl::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.
This is used when pop-up menu systems are active. All user events are
sent to the passed function, rather than going to widgets. The
void* argument can be used to pass arbitrary data to this
function.
This messes with the window system so you get the events no matter
where the cursor is on the screen. Under both X and WIN32 some
window must be mapped because the system interface needs a window id.
Be careful that your program does not enter an infinite loop
while grab() is on. On X this will lock up your screen!
To turn off grabbing do Fl::release().
Fl::grab() returns the current function, this is useful for
checking if a grab is active.
Returns the height of the screen in pixels.
Sends the event to a window for processing. Returns non-zero if any
widget uses the event.
This is the usage string that is displayed if Fl::args()
detects an invalid argument on the command-line.
Returns the top-most modal() window currently shown. This is
the most recently shown() window with modal() true, or
NULL if there are no modal() windows
shown(). The modal() window has its
handle() method called for all events, and no other windows
will have handle() called (grab()
overrides this).
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.
Set things up so the receiver widget will be called with an
FL_PASTE event some time in the future. 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 in FLTK.
Get or set the widget that is being pushed. FL_DRAG or
FL_RELEASE (and any more FL_PUSH) events will be sent to
this widget.
If you change the pushed widget, the previous one and all parents
(that don't contain the new widget) are sent FL_RELEASE
events. Changing this does not send FL_PUSH to this
or any widget, because sending FL_PUSH is supposed to test
if the widget wants the mouse (by it returning non-zero from
handle()).
This is similar to Fl::check() except this does not
call Fl::flush() or any callbacks, which is useful if your
program is in a state where such callbacks are illegal. This returns
true if Fl::check() would do anything (it will continue to
return true until you call Fl::check() or Fl::wait()).
while (!calculation_done()) {
calculate();
if (Fl::ready()) {
do_expensive_cleanup();
Fl::check();
if (user_hit_abort_button()) break;
}
}
Redraws all widgets.
Returns true if the specified idle callback is currently installed.
Removes the specified idle callback, if it is installed.
Returns true if the timeout exists and has not been called yet.
Removes a timeout callback. It is harmless to remove a timeout
callback that no longer exists.
Returns true if the check exists and has not been called yet.
Removes a check callback. It is harmless to remove a check
callback that no longer exists.
Calls Fl::wait() in an infinite loop. The only
way out is for a callback to call exit() or abort().
A normal program will end main() with return
Fl::run();.
The first form changes the current selection. The block of text is
copied to an internal buffer by FLTK (be careful if doing this in
response to an FL_PASTE as this may be the same buffer
returned by event_text()). The second form looks
at the buffer containing the current selection. The contents of this
buffer are undefined if this program does not own the current
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.
Test the current event, which must be an FL_KEYBOARD or
FL_SHORTCUT, against a shortcut value (described in Fl_Button).
Returns non-zero if there is a match. Not to be confused with Fl_Widget::test_shortcut().
Unparse a key name (as returned by Fl::event_key()) or a shortcut value (as
used by
Fl_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.
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:
- FL_INDEX indicates that a colormapped visual is ok. This
call will normally fail if a TrueColor visual cannot be found.
- FL_RGB this value is zero and may be passed to indicate
that FL_INDEX is not wanted.
- FL_RGB8 indicates that the TrueColor visual must have at
least 8 bits of red, green, and blue (Windows calls this "millions of
colors").
- FL_DOUBLE indicates that hardware accelerated double
buffering is wanted. This will make Fl_Double_Window work better.
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.
This does the same thing as Fl::visual(int) but also requires OpenGL
drawing to work. Doing this on X will reduce colormap flashing at the
edges of Fl_Gl_Windows when
they are inside regular windows.
See Fl_Gl_Window
for a list of additional values for the argument.
Returns the width of the screen in pixels.
Same as Fl::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.
static int Fl::wait(double 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 Fl::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 Fl::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.
FLTK will call these to print messages when unexpected conditions
occur. By default they fprintf to stderr, and
Fl::error and Fl::fatal call exit(1). You can
override the behavior by setting the function pointers to your own
routines.
Fl::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). Fl::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). Fl::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.