1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module glib.Idle;
26 
27 private import glib.Source;
28 private import gtkc.glib;
29 public  import gtkc.glibtypes;
30 
31 
32 public class Idle
33 {
34 	/** Holds all idle delegates */
35 	bool delegate()[] idleListeners;
36 	/** our idle ID */
37 	uint idleID;
38 	
39 	/**
40 	 * Creates a new idle cycle.
41 	 * Params:
42 	 *    	interval = the idle in milieconds
43 	 *    	dlg = the delegate to be executed
44 	 *    	fireNow = When true the delegate will be executed emmidiatly
45 	 */
46 	this(bool delegate() dlg, bool fireNow=false)
47 	{
48 		idleListeners ~= dlg;
49 		idleID = g_idle_add(cast(GSourceFunc)&idleCallback, cast(void*)this);
50 		if ( fireNow )
51 		{
52 			if ( !dlg() )
53 			{
54 				idleListeners.length = 0;
55 			}
56 		}
57 	}
58 	
59 	/**
60 	 * Creates a new idle cycle.
61 	 * Params:
62 	 *    	dlg = the delegate to be executed
63 	 *      priority = Priority for the idle function
64 	 *    	fireNow = When true the delegate will be executed emmidiatly
65 	 */
66 	this(bool delegate() dlg, GPriority priority, bool fireNow=false)
67 	{
68 		idleListeners ~= dlg;
69 		idleID = g_idle_add_full(priority, cast(GSourceFunc)&idleCallback, cast(void*)this, null);
70 		if ( fireNow )
71 		{
72 			if ( !dlg() )
73 			{
74 				idleListeners.length = 0;
75 			}
76 		}
77 	}
78 	
79 	/** */
80 	public void stop()
81 	{
82 		if ( idleID > 0 )
83 		{
84 			g_source_remove(idleID);
85 		}
86 		idleListeners.length = 0;
87 	}
88 	
89 	/**
90 	 * Removes the idle from gtk
91 	 */
92 	~this()
93 	{
94 		stop();
95 	}
96 	
97 	/**
98 	 * Adds a new delegate to this idle cycle
99 	 * Params:
100 	 *    	dlg =
101 	 *    	fireNow =
102 	 */
103 	public void addListener(bool delegate() dlg, bool fireNow=false)
104 	{
105 		idleListeners ~= dlg;
106 		if ( fireNow )
107 		{
108 			if ( !dlg() )
109 			{
110 				idleListeners.length = idleListeners.length - 1;
111 			}
112 		}
113 	}
114 	
115 	/**
116 	 * The callback execution from glib
117 	 * Params:
118 	 *    	idle =
119 	 * Returns:
120 	 */
121 	extern(C) static bool idleCallback(Idle idle)
122 	{
123 		return idle.callAllListeners();
124 	}
125 	
126 	/**
127 	 * Executes all delegates on the execution list
128 	 * Returns:
129 	 */
130 	private bool callAllListeners()
131 	{
132 		bool runAgain = false;
133 		
134 		int i = 0;
135 		
136 		while ( i<idleListeners.length )
137 		{
138 			if ( !idleListeners[i]() )
139 			{
140 				idleListeners = idleListeners[0..i] ~ idleListeners[i+1..idleListeners.length];
141 			}
142 			else
143 			{
144 				runAgain = true;
145 				++i;
146 			}
147 		}
148 		return runAgain;
149 	}
150 
151 	/**
152 	 */
153 
154 	/**
155 	 * Adds a function to be called whenever there are no higher priority
156 	 * events pending to the default main loop. The function is given the
157 	 * default idle priority, #G_PRIORITY_DEFAULT_IDLE.  If the function
158 	 * returns %FALSE it is automatically removed from the list of event
159 	 * sources and will not be called again.
160 	 *
161 	 * This internally creates a main loop source using g_idle_source_new()
162 	 * and attaches it to the main loop context using g_source_attach().
163 	 * You can do these steps manually if you need greater control.
164 	 *
165 	 * Params:
166 	 *     funct = function to call
167 	 *     data = data to pass to @function.
168 	 *
169 	 * Return: the ID (greater than 0) of the event source.
170 	 */
171 	public static uint add(GSourceFunc funct, void* data)
172 	{
173 		return g_idle_add(funct, data);
174 	}
175 
176 	/**
177 	 * Adds a function to be called whenever there are no higher priority
178 	 * events pending.  If the function returns %FALSE it is automatically
179 	 * removed from the list of event sources and will not be called again.
180 	 *
181 	 * This internally creates a main loop source using g_idle_source_new()
182 	 * and attaches it to the main loop context using g_source_attach().
183 	 * You can do these steps manually if you need greater control.
184 	 *
185 	 * Params:
186 	 *     priority = the priority of the idle source. Typically this will be in the
187 	 *         range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
188 	 *     funct = function to call
189 	 *     data = data to pass to @function
190 	 *     notify = function to call when the idle is removed, or %NULL
191 	 *
192 	 * Return: the ID (greater than 0) of the event source.
193 	 */
194 	public static uint addFull(int priority, GSourceFunc funct, void* data, GDestroyNotify notify)
195 	{
196 		return g_idle_add_full(priority, funct, data, notify);
197 	}
198 
199 	/**
200 	 * Removes the idle function with the given data.
201 	 *
202 	 * Params:
203 	 *     data = the data for the idle source's callback.
204 	 *
205 	 * Return: %TRUE if an idle source was found and removed.
206 	 */
207 	public static bool removeByData(void* data)
208 	{
209 		return g_idle_remove_by_data(data) != 0;
210 	}
211 
212 	/**
213 	 * Creates a new idle source.
214 	 *
215 	 * The source will not initially be associated with any #GMainContext
216 	 * and must be added to one with g_source_attach() before it will be
217 	 * executed. Note that the default priority for idle sources is
218 	 * %G_PRIORITY_DEFAULT_IDLE, as compared to other sources which
219 	 * have a default priority of %G_PRIORITY_DEFAULT.
220 	 *
221 	 * Return: the newly-created idle source
222 	 */
223 	public static Source sourceNew()
224 	{
225 		auto p = g_idle_source_new();
226 		
227 		if(p is null)
228 		{
229 			return null;
230 		}
231 		
232 		return new Source(cast(GSource*) p);
233 	}
234 }