summaryrefslogtreecommitdiff
path: root/src/Fl_mac.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'src/Fl_mac.cxx')
-rw-r--r--src/Fl_mac.cxx100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/Fl_mac.cxx b/src/Fl_mac.cxx
index 2338fd421..606570b69 100644
--- a/src/Fl_mac.cxx
+++ b/src/Fl_mac.cxx
@@ -599,6 +599,51 @@ static void breakMacEventLoop()
fl_unlock_function();
}
+//
+// MacOS X timers
+//
+
+struct MacTimeout {
+ Fl_Timeout_Handler callback;
+ void* data;
+ EventLoopTimerRef timer;
+};
+static MacTimeout* mac_timers;
+static int mac_timer_alloc;
+static int mac_timer_used;
+
+
+static void realloc_timers()
+{
+ if (mac_timer_alloc == 0) {
+ mac_timer_alloc = 8;
+ }
+ MacTimeout* new_timers = new MacTimeout[mac_timer_alloc * 2];
+ memmove(new_timers, mac_timers, sizeof(MacTimeout) * mac_timer_used);
+ MacTimeout* delete_me = mac_timers;
+ mac_timers = new_timers;
+ delete [] delete_me;
+ mac_timer_alloc *= 2;
+}
+
+static void delete_timer(MacTimeout& t)
+{
+ RemoveEventLoopTimer(t.timer);
+ memset(&t, 0, sizeof(MacTimeout));
+}
+
+
+static pascal void do_timer(EventLoopTimerRef timer, void* data)
+{
+ for (int i = 0; i < mac_timer_used; ++i) {
+ MacTimeout& t = mac_timers[i];
+ if (t.timer == timer && t.data == data) {
+ (*t.callback)(data);
+ break;
+ }
+ }
+ breakMacEventLoop();
+}
/**
* This function is the central event handler.
@@ -2136,6 +2181,61 @@ void Fl::paste(Fl_Widget &receiver, int clipboard) {
return;
}
+void Fl::add_timeout(double time, Fl_Timeout_Handler cb, void* data)
+{
+ int timer_id = -1;
+ for (int i = 0; i < mac_timer_used; ++i) {
+ if ( !mac_timers[i].timer ) {
+ timer_id = i;
+ break;
+ }
+ }
+ if (timer_id == -1) {
+ if (mac_timer_used == mac_timer_alloc) {
+ realloc_timers();
+ }
+ timer_id = mac_timer_used++;
+ }
+
+ EventTimerInterval fireDelay = (EventTimerInterval) time;
+ EventLoopTimerUPP timerUPP = NewEventLoopTimerUPP(do_timer);
+ EventLoopTimerRef timerRef;
+ OSStatus err = InstallEventLoopTimer(GetMainEventLoop(), fireDelay, 0, timerUPP, data, &timerRef);
+ if (err == noErr) {
+ mac_timers[timer_id].callback = cb;
+ mac_timers[timer_id].data = data;
+ mac_timers[timer_id].timer = timerRef;
+ }
+}
+
+void Fl::repeat_timeout(double time, Fl_Timeout_Handler cb, void* data)
+{
+ remove_timeout(cb, data);
+ add_timeout(time, cb, data);
+}
+
+int Fl::has_timeout(Fl_Timeout_Handler cb, void* data)
+{
+ for (int i = 0; i < mac_timer_used; ++i) {
+ MacTimeout& t = mac_timers[i];
+ if (t.callback == cb && t.data == data) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+void Fl::remove_timeout(Fl_Timeout_Handler cb, void* data)
+{
+ for (int i = 0; i < mac_timer_used; ++i) {
+ MacTimeout& t = mac_timers[i];
+ if (t.callback == cb && ( t.data == data || data == NULL)) {
+ delete_timer(t);
+ }
+ }
+}
+
+
//
// End of "$Id$".