//
// "$Id: Fl.cxx,v 1.191 2005/01/26 22:35:25 matthiaswm Exp $"
//
// Copyright 1998-2003 by Bill Spitzak and others.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//
// Please report all bugs and problems to "fltk-bugs@fltk.org".
//
#include <config.h>
#include <fltk/run.h>
#include <fltk/events.h>
#include <fltk/error.h>
#include <fltk/damage.h>
#include <fltk/layout.h>
#include <fltk/Window.h>
#include <fltk/Style.h>
#include <fltk/Tooltip.h>
#if defined(_WIN32) && USE_MULTIMONITOR && WINVER<0x0500
// Why??
# undef WINVER
# define WINVER 0x0500
#endif
#include <fltk/x.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
// things that should be in different source files:
#include <fltk/FL_VERSION.h>
#include <fltk/Monitor.h>
using namespace fltk;
/*! \namespace fltk
\brief All classes and interface functions for fltk are in the fltk:: namespace. This is a complete list of them, with links to the pages they are described on.
Put "using namespace fltk;" and you will not need to put the "fltk::" in
front of everything.
Fltk also provides some \ref utilities that are \e not in the fltk
namespace, as they are provided to make up for missing functions
that should be in standard libraries.
Fltk1.1 emulation is mostly achieved by aliasing the old class name
"Fl" to this namespace so that the static methods in Fltk1.1 will
work, and by typedefs from the new fltk::Widget class names to the
older Fl_Widget class names.
*/
/*!
Returns the version number of fltk. This can be compared to the
value of the FL_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:
- FL_MAJOR_VERSION - The major release number, currently 2.
- FL_MINOR_VERSION - The minor release number, currently 0.
- FL_PATCH_VERSION - The patch release number, currently 1.
- FL_VERSION - A combined floating-point version number of the
form M.mmpp where M is the major number, mm is the minor number,
and pp is the patch number, currently 2.0001.
*/
float fltk::version() {return FL_VERSION;}
//
// Globals...
//
Widget *fltk::belowmouse_,
*fltk::pushed_,
*fltk::focus_,
*fltk::modal_;
int fltk::e_type,
fltk::e_x,
fltk::e_y,
fltk::e_dx,
fltk::e_dy,
fltk::e_x_root,
fltk::e_y_root,
fltk::e_clicks,
fltk::e_device = DEVICE_MOUSE;
unsigned fltk::e_state,
fltk::e_keysym,
fltk::e_is_click,
fltk::e_length;
float fltk::e_pressure,
fltk::e_x_tilt,
fltk::e_y_tilt;
const char *fltk::e_text = "";
bool fltk::grab_,
fltk::exit_modal_;
static Window *xfocus; // which window X thinks has focus
static Window *xmousewin; // which window X thinks has ENTER
// Update focus() in response to anything that might change it.
// This is called whenever a window is added or hidden, and whenever
// X says the focus window has changed.
static void fix_focus() {
Widget* w = xfocus;
// Modal overrides whatever the system says the focus is:
if (grab_ || w && modal_) w = modal_;
if (w) {
if (w->contains(focus())) return; // already has it
unsigned saved = e_keysym;
e_keysym = 0; // make widgets not think a keystroke moved focus
if (w->take_focus()) {e_keysym = saved; return;}
e_keysym = saved;
}
// give nothing the focus:
focus(0);
}
#if USE_X11
# include "Fl_x.cxx"
#elif defined(_WIN32)
# include "Fl_win32.cxx"
#elif USE_QUARTZ
# include "Fl_mac.cxx"
#else
# error
#endif
////////////////////////////////////////////////////////////////
// Timeouts are stored in a sorted list, so only the first one needs
// to be checked to see if any should be called.
struct Timeout {
float time;
void (*cb)(void*);
void* arg;
Timeout* next;
};
static Timeout* first_timeout, *free_timeout;
#ifndef _WIN32
#include <sys/time.h>
#endif
// I avoid the overhead of getting the current time when we have no
// timeouts by setting this flag instead of getting the time.
// In this case calling elapse_timeouts() does nothing, but records
// the current time, and the next call will actualy elapse time.
static char reset_clock = 1;
static void elapse_timeouts() {
#ifdef _WIN32
unsigned long newclock = GetTickCount();
static unsigned long prevclock;
float elapsed = (newclock-prevclock)/1000.0f;
prevclock = newclock;
#else
static struct timeval prevclock;
struct timeval newclock;
gettimeofday(&newclock, NULL);
float elapsed = float(newclock.tv_sec - prevclock.tv_sec) +
float(newclock.tv_usec - prevclock.tv_usec)/1000000.0f;
prevclock.tv_sec = newclock.tv_sec;
prevclock.tv_usec = newclock.tv_usec;
#endif
if (reset_clock) {
reset_clock = 0;
} else if (elapsed > 0) {
for (Timeout* t = first_timeout; t; t = t->next) t->time -= elapsed;
}
}
// Continuously-adjusted error value, this is a number <= 0 for how late
// we were at calling the last timeout. This appears to make repeat_timeout
// very accurate even when processing takes a significant portion of the
// time interval:
static float missed_timeout_by;
/*!
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::add_timeout(float time, TimeoutHandler cb, void *arg) {
elapse_timeouts();
repeat_timeout(time, cb, arg);
}
/*!
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).
Outside a timeout callback this acts like add_timeout().
This code will print "TICK" each second on stdout, with a fair
degree of accuracy:
\code
void callback(void*) {
printf("TICK\n");
fltk::repeat_timeout(1.0,callback);
}
main() {
fltk::add_timeout(1.0,callback);
for (;;) fltk::wait();
}
\endcode
*/
void fltk::repeat_timeout(float time, TimeoutHandler cb, void *arg) {
time += missed_timeout_by; if (time < -.05f) time = 0;
Timeout* t = free_timeout;
if (t) free_timeout = t->next;
else t = new Timeout;
t->time = time;
t->cb = cb;
t->arg = arg;
// insert-sort the new timeout:
Timeout** p = &first_timeout;
while (*p && (*p)->time <= time) p = &((*p)->next);
t->next = *p;
*p = t;
}
/*!
Returns true if the timeout exists and has not been called yet.
*/
bool fltk::has_timeout(TimeoutHandler cb, void *arg) {
for (Timeout* t = first_timeout; t; t = t->next)
if (t->cb == cb && t->arg == arg) return true;
return false;
}
/*!
Removes all pending timeout callbacks that match the function and arg.
Does nothing if there are no matching ones that have not been
called yet.
*/
void fltk::remove_timeout(TimeoutHandler cb, void *arg) {
for (Timeout** p = &first_timeout; *p;) {
Timeout* t = *p;
if (t->cb == cb && t->arg == arg) {
*p = t->next;
t->next = free_timeout;
free_timeout = t;
} else {
p = &(t->next);
}
}
}
////////////////////////////////////////////////////////////////
// Checks are just stored in a list. They are called in the reverse
// order that they were added (this may change in the future).
// This is a bit messy because I want to allow checks to be added,
// removed, and have wait() called from inside them, to do this
// next_check points at the next unprocessed one for the outermost
// call to wait().
struct Check {
void (*cb)(void*);
void* arg;
Check* next;
};
static Check* first_check, *next_check, *free_check;
/*!
Fltk will call this callback just before it flushes the display and
waits for events. This is different than add_idle() because it
is only called once, then fltk calls the system and tells it not to
return until an event happens. If several checks have been added fltk
calls them all, the most recently added one first.
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:
\code
bool state_changed; // anything that changes the display turns this on
void check(void*) {
if (!state_changed) return;
state_changed = false;
do_expensive_calculation();
widget->redraw();
}
main() {
fltk::add_check(1.0,check);
return fltk::run();
}
\endcode
*/
void fltk::add_check(TimeoutHandler cb, void *arg) {
Check* t = free_check;
if (t) free_check = t->next;
else t = new Check;
t->cb = cb;
t->arg = arg;
t->next = first_check;
if (next_check == first_check) next_check = t;
first_check = t;
}
/*!
Remove all matching check callback, if any exists. You can call this
from inside the check callback if you want.
*/
void fltk::remove_check(TimeoutHandler cb, void *arg) {
for (Check** p = &first_check; *p;) {
Check* t = *p;
if (t->cb == cb && t->arg == arg) {
if (next_check == t) next_check = t->next;
*p = t->next;
t->next = free_check;
free_check = t;
} else {
p = &(t->next);
}
}
}
/*!
Return true if add_check() has been done with this \a cb and \a arg,
and remove_check() has not been done.
*/
bool fltk::has_check(TimeoutHandler cb, void *arg) {
for (Check* t = first_check; t; t = t->next)
if (t->cb == cb && t->arg == arg) return true;
return false;
}
////////////////////////////////////////////////////////////////
// wait/run/check/ready:
void (*fltk::idle)(); // see add_idle.cxx for the add/remove functions
static bool in_idle;
#define FOREVER 1e20f
/*!
Calls fltk::wait() as long as any windows are not closed. When
all the windows are hidden or destroyed (checked by seeing if
Window::first() is null) 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();.
*/
int fltk::run() {
while (Window::first()) wait(FOREVER);
return(0);
// WAS: This was tried for fltk 2.0, and the callback for closing the last
// window in Window.C called exit(). This proved to be unpopular:
// for (;;) wait(FOREVER);
}
/*!
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() {
return wait(FOREVER);
}
static void run_checks() {
// checks are a bit messy so that add/remove and wait may be called
// from inside them without causing an infinite loop. We must also
// do them first so that they can install an idle or timeout function:
if (next_check == first_check) {
while (next_check) {
Check* check = next_check;
next_check = check->next;
(check->cb)(check->arg);
}
next_check = first_check;
}
}
/*!
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 zero if nothing happened before the passed
\a time_to_wait expired. It is non-zero if any events or timeouts
came in.
*/
int fltk::wait(float time_to_wait) {
// check functions must be run first so they can install idle or timeout
// functions:
run_checks();
flush();
if (first_timeout) {
elapse_timeouts();
Timeout* t = first_timeout;
if (t->time < time_to_wait) time_to_wait = t->time;
}
// run the system-specific part that waits for sockets & events:
if (time_to_wait <= 0 || idle && !in_idle) time_to_wait = 0;
int ret = fl_wait(time_to_wait);
if (first_timeout) {
elapse_timeouts();
Timeout *t;
while ((t = first_timeout)) {
if (t->time > 0) break;
// The first timeout in the array has expired.
missed_timeout_by = t->time;
// We must remove timeout from array before doing the callback:
void (*cb)(void*) = t->cb;
void *arg = t->arg;
first_timeout = t->next;
t->next = free_timeout;
free_timeout = t;
// Now it is safe for the callback to do add_timeout:
cb(arg);
// return true because something was done:
ret = 1;
}
} else {
reset_clock = 1; // remember that elapse_timeouts was not called
}
if (idle && !in_idle) {in_idle = true; idle(); in_idle = false;}
flush();
return ret;
}
/*!
Same as fltk::wait(0). Calling this during a big calculation will
keep the screen up to date and the interface responsive:
\code
while (!calculation_done()) {
calculate();
fltk::check();
if (user_hit_abort_button()) break;
}
\endcode
*/
int fltk::check() {
return wait(0.0);
}
/*!
Test to see if any events or callbacks are pending. This will
return true if fltk::check() would do anything other than update
the screen. Since this will not draw anything or call any code,
it is safe to call this if your program is in an inconsistent
state. This is also useful if your calculation is updating widgets but
you do not want or need the overhead of the screen updating every time
you check for events.
\code
while (!calculation_done()) {
calculate();
if (fltk::ready()) {
do_expensive_cleanup();
fltk::check();
if (user_hit_abort_button()) break;
}
}
\endcode
*/
int fltk::ready() {
if (first_timeout) {
elapse_timeouts();
if (first_timeout->time <= 0) return 1;
} else {
reset_clock = 1;
}
// run the system-specific part:
return fl_ready();
}
////////////////////////////////////////////////////////////////
// Window list management:
CreatedWindow* CreatedWindow::first;
#if USE_X11
Window* fltk::find(XWindow xid)
#elif defined(_WIN32)
Window* fltk::find(HWND xid)
#elif USE_QUARTZ
Window* fltk::find(WindowPtr xid)
#endif
{
CreatedWindow *x;
for (CreatedWindow **pp = &CreatedWindow::first; (x = *pp); pp = &x->next)
#if USE_X11
if (x->xid == xid || x->frontbuffer==xid)
#else
if (x->xid == xid)
#endif
{if (x != CreatedWindow::first) {
// make this window be first to speed up searches
*pp = x->next;
x->next = CreatedWindow::first;
CreatedWindow::first = x;
}
return x->window;
}
return 0;
}
/*!
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
as a default value for fltk::Window::child_of().
fltk::Window::exec() uses it for this if no other parent is specified.
This is also used by fltk::run() to see if any windows still exist.
*/
Window* Window::first() {
for (CreatedWindow* x = CreatedWindow::first;; x = x->next) {
if (!x) return 0;
Window* window = x->window;
if (window->visible() && !window->parent() && !window->override())
return window;
}
}
/*!
Returns the next visible() top-level window, returns NULL after
the last one. You can use this and first() to iterate through
all the visible windows.
*/
Window* Window::next() {
for (CreatedWindow* x = CreatedWindow::find(this)->next;; x = x->next) {
if (!x) return 0;
Window* window = x->window;
if (window->visible() && !window->parent() && !window->override())
return 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().
*/
void Window::first(Window* window) {
if (!window || !window->shown()) return;
fltk::find(xid(window));
}
int fltk::damage_;
/*! \fn int fltk::damage()
True if any Widget::redraw() calls have been done since the
last fltk::flush(). This indicates that flush() will do something.
Currently the meaning of any bits are undefined.
Window flush() routines can set this to indicate that flush() should
be called again after waiting for more events. This is useful in
some instances such as X windows that are waiting for a mapping
event before being drawn.
*/
/*!
Redraws all widgets. This is a good idea if you have made global
changes to the styles.
*/
void fltk::redraw() {
for (CreatedWindow* x = CreatedWindow::first; x; x = x->next)
x->window->redraw();
}
#if !USE_X11 && defined(_WIN32)
extern void fl_do_deferred_calls(); // in Fl_Window.cxx:
#endif
/*!
Get the display up to date. This is done by calling layout() on
all Window objects with layout_damage() and then calling draw()
on all Window objects with damage(). (actually it calls Window::flush()
and that calls draw(), but normally you can ignore this). This
will also flush the X i/o buffer, update the cursor shape, update
Windows window sizes, and other operations to get the display
up to date.
wait() calls this before it waits for events.
*/
void fltk::flush() {
#if USE_X11
if (!xdisplay) return; // ignore if no windows created yet
#elif defined(_WIN32)
fl_do_deferred_calls();
#endif
if (damage_) {
damage_ = false; // turn it off so Window::flush() can turn it back on
for (CreatedWindow* x = CreatedWindow::first; x; x = x->next) {
if (x->wait_for_expose) {damage_ = true; continue;}
Window* window = x->window;
if (window->visible_r() && window->w()>0 &&