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 	 * See [memory management of sources][mainloop-memory-management] for details
162 	 * on how to handle the return value and memory management of @data.
163 	 *
164 	 * This internally creates a main loop source using g_idle_source_new()
165 	 * and attaches it to the global #GMainContext using g_source_attach(), so
166 	 * the callback will be invoked in whichever thread is running that main
167 	 * context. You can do these steps manually if you need greater control or to
168 	 * use a custom main context.
169 	 *
170 	 * Params:
171 	 *     funct = function to call
172 	 *     data = data to pass to @function.
173 	 *
174 	 * Return: the ID (greater than 0) of the event source.
175 	 */
176 	public static uint add(GSourceFunc funct, void* data)
177 	{
178 		return g_idle_add(funct, data);
179 	}
180 
181 	/**
182 	 * Adds a function to be called whenever there are no higher priority
183 	 * events pending.  If the function returns %FALSE it is automatically
184 	 * removed from the list of event sources and will not be called again.
185 	 *
186 	 * See [memory management of sources][mainloop-memory-management] for details
187 	 * on how to handle the return value and memory management of @data.
188 	 *
189 	 * This internally creates a main loop source using g_idle_source_new()
190 	 * and attaches it to the global #GMainContext using g_source_attach(), so
191 	 * the callback will be invoked in whichever thread is running that main
192 	 * context. You can do these steps manually if you need greater control or to
193 	 * use a custom main context.
194 	 *
195 	 * Params:
196 	 *     priority = the priority of the idle source. Typically this will be in the
197 	 *         range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
198 	 *     funct = function to call
199 	 *     data = data to pass to @function
200 	 *     notify = function to call when the idle is removed, or %NULL
201 	 *
202 	 * Return: the ID (greater than 0) of the event source.
203 	 */
204 	public static uint addFull(int priority, GSourceFunc funct, void* data, GDestroyNotify notify)
205 	{
206 		return g_idle_add_full(priority, funct, data, notify);
207 	}
208 
209 	/**
210 	 * Removes the idle function with the given data.
211 	 *
212 	 * Params:
213 	 *     data = the data for the idle source's callback.
214 	 *
215 	 * Return: %TRUE if an idle source was found and removed.
216 	 */
217 	public static bool removeByData(void* data)
218 	{
219 		return g_idle_remove_by_data(data) != 0;
220 	}
221 
222 	/**
223 	 * Creates a new idle source.
224 	 *
225 	 * The source will not initially be associated with any #GMainContext
226 	 * and must be added to one with g_source_attach() before it will be
227 	 * executed. Note that the default priority for idle sources is
228 	 * %G_PRIORITY_DEFAULT_IDLE, as compared to other sources which
229 	 * have a default priority of %G_PRIORITY_DEFAULT.
230 	 *
231 	 * Return: the newly-created idle source
232 	 */
233 	public static Source sourceNew()
234 	{
235 		auto p = g_idle_source_new();
236 		
237 		if(p is null)
238 		{
239 			return null;
240 		}
241 		
242 		return new Source(cast(GSource*) p);
243 	}
244 }