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