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 /** */
33 public class Idle
34 {
35 	/** Holds all idle delegates */
36 	bool delegate()[] idleListeners;
37 	/** our idle ID */
38 	uint idleID;
39 	
40 	/**
41 	 * Creates a new idle cycle.
42 	 * Params:
43 	 *    	interval = the idle in milieconds
44 	 *    	dlg = the delegate to be executed
45 	 *    	fireNow = When true the delegate will be executed emmidiatly
46 	 */
47 	this(bool delegate() dlg, bool fireNow=false)
48 	{
49 		idleListeners ~= dlg;
50 		idleID = g_idle_add(cast(GSourceFunc)&idleCallback, cast(void*)this);
51 		if ( fireNow )
52 		{
53 			if ( !dlg() )
54 			{
55 				idleListeners.length = 0;
56 			}
57 		}
58 	}
59 	
60 	/**
61 	 * Creates a new idle cycle.
62 	 * Params:
63 	 *    	dlg = the delegate to be executed
64 	 *      priority = Priority for the idle function
65 	 *    	fireNow = When true the delegate will be executed emmidiatly
66 	 */
67 	this(bool delegate() dlg, GPriority priority, bool fireNow=false)
68 	{
69 		idleListeners ~= dlg;
70 		idleID = g_idle_add_full(priority, cast(GSourceFunc)&idleCallback, cast(void*)this, null);
71 		if ( fireNow )
72 		{
73 			if ( !dlg() )
74 			{
75 				idleListeners.length = 0;
76 			}
77 		}
78 	}
79 	
80 	/** */
81 	public void stop()
82 	{
83 		if ( idleID > 0 )
84 		{
85 			g_source_remove(idleID);
86 		}
87 		idleListeners.length = 0;
88 	}
89 	
90 	/**
91 	 * Removes the idle from gtk
92 	 */
93 	~this()
94 	{
95 		stop();
96 	}
97 	
98 	/**
99 	 * Adds a new delegate to this idle cycle
100 	 * Params:
101 	 *    	dlg =
102 	 *    	fireNow =
103 	 */
104 	public void addListener(bool delegate() dlg, bool fireNow=false)
105 	{
106 		idleListeners ~= dlg;
107 		if ( fireNow )
108 		{
109 			if ( !dlg() )
110 			{
111 				idleListeners.length = idleListeners.length - 1;
112 			}
113 		}
114 	}
115 	
116 	/**
117 	 * The callback execution from glib
118 	 * Params:
119 	 *    	idle =
120 	 * Returns:
121 	 */
122 	extern(C) static bool idleCallback(Idle idle)
123 	{
124 		return idle.callAllListeners();
125 	}
126 	
127 	/**
128 	 * Executes all delegates on the execution list
129 	 * Returns:
130 	 */
131 	private bool callAllListeners()
132 	{
133 		bool runAgain = false;
134 		
135 		int i = 0;
136 		
137 		while ( i<idleListeners.length )
138 		{
139 			if ( !idleListeners[i]() )
140 			{
141 				idleListeners = idleListeners[0..i] ~ idleListeners[i+1..idleListeners.length];
142 			}
143 			else
144 			{
145 				runAgain = true;
146 				++i;
147 			}
148 		}
149 		
150 		// Set idleID to 0 if all delegates are removed
151 		if (idleListeners.length == 0)
152 			idleID = 0;
153 		
154 		return runAgain;
155 	}
156 
157 	/**
158 	 */
159 
160 	/**
161 	 * Adds a function to be called whenever there are no higher priority
162 	 * events pending to the default main loop. The function is given the
163 	 * default idle priority, #G_PRIORITY_DEFAULT_IDLE.  If the function
164 	 * returns %FALSE it is automatically removed from the list of event
165 	 * sources and will not be called again.
166 	 *
167 	 * See [memory management of sources][mainloop-memory-management] for details
168 	 * on how to handle the return value and memory management of @data.
169 	 *
170 	 * This internally creates a main loop source using g_idle_source_new()
171 	 * and attaches it to the global #GMainContext using g_source_attach(), so
172 	 * the callback will be invoked in whichever thread is running that main
173 	 * context. You can do these steps manually if you need greater control or to
174 	 * use a custom main context.
175 	 *
176 	 * Params:
177 	 *     funct = function to call
178 	 *     data = data to pass to @function.
179 	 *
180 	 * Returns: the ID (greater than 0) of the event source.
181 	 */
182 	public static uint add(GSourceFunc funct, void* data)
183 	{
184 		return g_idle_add(funct, data);
185 	}
186 
187 	/**
188 	 * Adds a function to be called whenever there are no higher priority
189 	 * events pending.  If the function returns %FALSE it is automatically
190 	 * removed from the list of event sources and will not be called again.
191 	 *
192 	 * See [memory management of sources][mainloop-memory-management] for details
193 	 * on how to handle the return value and memory management of @data.
194 	 *
195 	 * This internally creates a main loop source using g_idle_source_new()
196 	 * and attaches it to the global #GMainContext using g_source_attach(), so
197 	 * the callback will be invoked in whichever thread is running that main
198 	 * context. You can do these steps manually if you need greater control or to
199 	 * use a custom main context.
200 	 *
201 	 * Params:
202 	 *     priority = the priority of the idle source. Typically this will be in the
203 	 *         range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
204 	 *     funct = function to call
205 	 *     data = data to pass to @function
206 	 *     notify = function to call when the idle is removed, or %NULL
207 	 *
208 	 * Returns: the ID (greater than 0) of the event source.
209 	 */
210 	public static uint addFull(int priority, GSourceFunc funct, void* data, GDestroyNotify notify)
211 	{
212 		return g_idle_add_full(priority, funct, data, notify);
213 	}
214 
215 	/**
216 	 * Removes the idle function with the given data.
217 	 *
218 	 * Params:
219 	 *     data = the data for the idle source's callback.
220 	 *
221 	 * Returns: %TRUE if an idle source was found and removed.
222 	 */
223 	public static bool removeByData(void* data)
224 	{
225 		return g_idle_remove_by_data(data) != 0;
226 	}
227 
228 	/**
229 	 * Creates a new idle source.
230 	 *
231 	 * The source will not initially be associated with any #GMainContext
232 	 * and must be added to one with g_source_attach() before it will be
233 	 * executed. Note that the default priority for idle sources is
234 	 * %G_PRIORITY_DEFAULT_IDLE, as compared to other sources which
235 	 * have a default priority of %G_PRIORITY_DEFAULT.
236 	 *
237 	 * Returns: the newly-created idle source
238 	 */
239 	public static Source sourceNew()
240 	{
241 		auto p = g_idle_source_new();
242 		
243 		if(p is null)
244 		{
245 			return null;
246 		}
247 		
248 		return new Source(cast(GSource*) p, true);
249 	}
250 }