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