/*
* Licensed under the GNU Lesser General Public License Version 3
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the license, or
* (at your option) any later version.
*
* This software 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see .
*/
// generated automatically - do not change
module glib.Timeout;
private import glib.Source;
private import glib.c.functions;
public import glib.c.types;
/** */
public class Timeout
{
/** Holds all timeout delegates */
bool delegate()[] timeoutListeners;
/** our gtk timeout ID */
uint timeoutID;
/**
* Creates a new timeout cycle with the default priority, GPriority.DEFAULT.
*
* Note that timeout functions may be delayed, due to the processing of other
* event sources. Thus they should not be relied on for precise timing.
* After each call to the timeout function, the time of the next timeout is
* recalculated based on the current time and the given interval
* (it does not try to 'catch up' time lost in delays).
* Params:
* interval = the timeout in milieconds
* delegate() = the delegate to be executed
* fireNow = When true the delegate will be executed emmidiatly
*/
this(uint interval, bool delegate() dlg, bool fireNow=false)
{
timeoutListeners ~= dlg;
timeoutID = g_timeout_add(interval, cast(GSourceFunc)&timeoutCallback, cast(void*)this);
if ( fireNow )
{
if ( !dlg() )
{
timeoutListeners.length = 0;
}
}
}
/**
* Creates a new timeout cycle.
* Params:
* interval = the timeout in milieconds
* delegate() = the delegate to be executed
* priority = Priority for the timeout function
* fireNow = When true the delegate will be executed emmidiatly
*/
this(uint interval, bool delegate() dlg, GPriority priority, bool fireNow=false)
{
timeoutListeners ~= dlg;
timeoutID = g_timeout_add_full(priority, interval, cast(GSourceFunc)&timeoutCallback, cast(void*)this, null);
if ( fireNow )
{
if ( !dlg() )
{
timeoutListeners.length = 0;
}
}
}
/**
* Creates a new timeout cycle with the default priority, GPriority.DEFAULT.
* Params:
* delegate() = the delegate to be executed
* seconds = interval in seconds.
* fireNow = When true the delegate will be executed emmidiatly
*/
this(bool delegate() dlg, uint seconds, bool fireNow=false)
{
timeoutListeners ~= dlg;
timeoutID = g_timeout_add_seconds(seconds, cast(GSourceFunc)&timeoutCallback, cast(void*)this);
if ( fireNow )
{
if ( !dlg() )
{
timeoutListeners.length = 0;
}
}
}
/**
* Creates a new timeout cycle.
* Params:
* delegate() = the delegate to be executed
* seconds = interval in seconds.
* priority = Priority for the timeout function
* fireNow = When true the delegate will be executed emmidiatly
*/
this(bool delegate() dlg, uint seconds, GPriority priority, bool fireNow=false)
{
timeoutListeners ~= dlg;
timeoutID = g_timeout_add_seconds_full(priority, seconds, cast(GSourceFunc)&timeoutCallback, cast(void*)this, null);
if ( fireNow )
{
if ( !dlg() )
{
timeoutListeners.length = 0;
}
}
}
/** */
public void stop()
{
if ( timeoutID > 0 )
{
g_source_remove(timeoutID);
}
timeoutID = 0;
timeoutListeners.length = 0;
}
/**
* Removes the timeout from gtk
*/
~this()
{
stop();
}
/**
* Adds a new delegate to this timeout cycle
* Params:
* dlg =
* fireNow =
*/
public void addListener(bool delegate() dlg, bool fireNow=false)
{
timeoutListeners ~= dlg;
if ( fireNow )
{
if ( !dlg() )
{
timeoutListeners.length = timeoutListeners.length - 1;
}
}
}
/**
* The callback execution from glib
* Params:
* timeout =
* Returns:
*/
extern(C) static bool timeoutCallback(Timeout timeout)
{
return timeout.callAllListeners();
}
/**
* Executes all delegates on the execution list
* Returns:
*/
private bool callAllListeners()
{
bool runAgain = false;
int i = 0;
while ( i