themes: Workaround for bug where a background color of RGB 0,0,0 in Black color schem...
[ntk.git] / src / Fl_add_idle.cxx
blob6059ae1c1914027ad3114940138b71febe785c9a
1 //
2 // "$Id: Fl_add_idle.cxx 7903 2010-11-28 21:06:39Z matt $"
3 //
4 // Idle routine support for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 by Bill Spitzak and others.
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 // USA.
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
28 // Allows you to manage an arbitrary set of idle() callbacks.
29 // Replaces the older set_idle() call (which is used to implement this)
31 #include <FL/Fl.H>
33 struct idle_cb {
34 void (*cb)(void*);
35 void* data;
36 idle_cb *next;
39 // the callbacks are stored linked in a ring. last points at the one
40 // just called, first at the next to call. last->next == first.
42 static idle_cb* first;
43 static idle_cb* last;
44 static idle_cb* freelist;
46 static void call_idle() {
47 idle_cb* p = first;
48 last = p; first = p->next;
49 p->cb(p->data); // this may call add_idle() or remove_idle()!
52 /**
53 Adds a callback function that is called every time by Fl::wait() and also
54 makes it act as though the timeout is zero (this makes Fl::wait() return
55 immediately, so if it is in a loop it is called repeatedly, and thus the
56 idle fucntion is called repeatedly). The idle function can be used to get
57 background processing done.
59 You can have multiple idle callbacks. To remove an idle callback use
60 Fl::remove_idle().
62 Fl::wait() and Fl::check() call idle callbacks, but Fl::ready() does not.
64 The idle callback can call any FLTK functions, including Fl::wait(),
65 Fl::check(), and Fl::ready().
67 FLTK will not recursively call the idle callback.
69 void Fl::add_idle(Fl_Idle_Handler cb, void* data) {
70 idle_cb* p = freelist;
71 if (p) freelist = p->next;
72 else p = new idle_cb;
73 p->cb = cb;
74 p->data = data;
75 if (first) {
76 last->next = p;
77 last = p;
78 p->next = first;
79 } else {
80 first = last = p;
81 p->next = p;
82 set_idle(call_idle);
86 /**
87 Returns true if the specified idle callback is currently installed.
89 int Fl::has_idle(Fl_Idle_Handler cb, void* data) {
90 idle_cb* p = first;
91 if (!p) return 0;
92 for (;; p = p->next) {
93 if (p->cb == cb && p->data == data) return 1;
94 if (p==last) return 0;
98 /**
99 Removes the specified idle callback, if it is installed.
101 void Fl::remove_idle(Fl_Idle_Handler cb, void* data) {
102 idle_cb* p = first;
103 if (!p) return;
104 idle_cb* l = last;
105 for (;; p = p->next) {
106 if (p->cb == cb && p->data == data) break;
107 if (p==last) return; // not found
108 l = p;
110 if (l == p) { // only one
111 first = last = 0;
112 set_idle(0);
113 } else {
114 last = l;
115 first = l->next = p->next;
117 p->next = freelist;
118 freelist = p;
122 // End of "$Id: Fl_add_idle.cxx 7903 2010-11-28 21:06:39Z matt $".