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 glib.c.functions;
29 public  import glib.c.types;
30 
31 
32 /** */
33 public class Idle
34 {
35 	/** Holds all idle delegates */
36 	private bool delegate()[] idleListeners;
37 	/** Our idle ID */
38 	private uint idleID;
39 	/** The priority this class was instantiated with */
40 	private GPriority priority = GPriority.DEFAULT_IDLE;
41 
42 	/**
43 	 * Creates a new idle cycle.
44 	 * Params:
45 	 *    	dlg = the delegate to be executed
46 	 *    	fireNow = When true the delegate will be executed emmidiatly
47 	 */
48 	this(bool delegate() dlg, bool fireNow=false)
49 	{
50 		if ( fireNow && !dlg() )
51 			return;
52 
53 		idleListeners ~= dlg;
54 		idleID = g_idle_add_full(priority, cast(GSourceFunc)&idleCallback, cast(void*)this, cast(GDestroyNotify)&destroyIdleNotify);
55 	}
56 
57 	/**
58 	 * Creates a new idle cycle.
59 	 * Params:
60 	 *    	dlg = the delegate to be executed
61 	 *      priority = Priority for the idle function
62 	 *    	fireNow = When true the delegate will be executed emmidiatly
63 	 */
64 	this(bool delegate() dlg, GPriority priority, bool fireNow=false)
65 	{
66 		this.priority = priority;
67 
68 		if ( fireNow && !dlg() )
69 			return;
70 
71 		idleListeners ~= dlg;
72 		idleID = g_idle_add_full(priority, cast(GSourceFunc)&idleCallback, cast(void*)this, cast(GDestroyNotify)&destroyIdleNotify);
73 	}
74 
75 	/** */
76 	public void stop()
77 	{
78 		if ( idleID > 0 )
79 			g_source_remove(idleID);
80 	}
81 
82 	/**
83 	 * Removes the idle from gtk
84 	 */
85 	~this()
86 	{
87 		stop();
88 	}
89 
90 	/**
91 	 * Adds a new delegate to this idle cycle
92 	 * Params:
93 	 *    	dlg =
94 	 *    	fireNow =
95 	 */
96 	public void addListener(bool delegate() dlg, bool fireNow=false)
97 	{
98 		if ( fireNow && !dlg() )
99 			return;
100 
101 		idleListeners ~= dlg;
102 
103 		if ( idleID == 0 )
104 			idleID = g_idle_add_full(priority, cast(GSourceFunc)&idleCallback, cast(void*)this, cast(GDestroyNotify)&destroyIdleNotify);
105 	}
106 
107 	/*
108 	 * Executes all delegates on the execution list
109 	 * Returns: false if the callback should be removed.
110 	 */
111 	extern(C) static bool idleCallback(Idle idle)
112 	{
113 		bool runAgain = false;
114 		int i = 0;
115 
116 		while ( i<idle.idleListeners.length )
117 		{
118 			if ( !idle.idleListeners[i]() )
119 			{
120 				idle.idleListeners = idle.idleListeners[0..i] ~ idle.idleListeners[i+1..$];
121 			}
122 			else
123 			{
124 				runAgain = true;
125 				++i;
126 			}
127 		}
128 
129 		return runAgain;
130 	}
131 
132 	/*
133 	 * Reset the idle object when it's destroyed on the GTK side.
134 	 */
135 	extern(C) static void destroyIdleNotify(Idle idle)
136 	{
137 		idle.idleListeners.length = 0;
138 		idle.idleID = 0;
139 	}
140 
141 	/**
142 	 */
143 
144 	/**
145 	 * Adds a function to be called whenever there are no higher priority
146 	 * events pending to the default main loop. The function is given the
147 	 * default idle priority, #G_PRIORITY_DEFAULT_IDLE.  If the function
148 	 * returns %FALSE it is automatically removed from the list of event
149 	 * sources and will not be called again.
150 	 *
151 	 * See [memory management of sources][mainloop-memory-management] for details
152 	 * on how to handle the return value and memory management of @data.
153 	 *
154 	 * This internally creates a main loop source using g_idle_source_new()
155 	 * and attaches it to the global #GMainContext using g_source_attach(), so
156 	 * the callback will be invoked in whichever thread is running that main
157 	 * context. You can do these steps manually if you need greater control or to
158 	 * use a custom main context.
159 	 *
160 	 * Params:
161 	 *     function_ = function to call
162 	 *     data = data to pass to @function.
163 	 *
164 	 * Returns: the ID (greater than 0) of the event source.
165 	 */
166 	public static uint add(GSourceFunc function_, void* data)
167 	{
168 		return g_idle_add(function_, data);
169 	}
170 
171 	/**
172 	 * Adds a function to be called whenever there are no higher priority
173 	 * events pending.  If the function returns %FALSE it is automatically
174 	 * removed from the list of event sources and will not be called again.
175 	 *
176 	 * See [memory management of sources][mainloop-memory-management] for details
177 	 * on how to handle the return value and memory management of @data.
178 	 *
179 	 * This internally creates a main loop source using g_idle_source_new()
180 	 * and attaches it to the global #GMainContext using g_source_attach(), so
181 	 * the callback will be invoked in whichever thread is running that main
182 	 * context. You can do these steps manually if you need greater control or to
183 	 * use a custom main context.
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 	 *     function_ = function to call
189 	 *     data = data to pass to @function
190 	 *     notify = function to call when the idle is removed, or %NULL
191 	 *
192 	 * Returns: the ID (greater than 0) of the event source.
193 	 */
194 	public static uint addFull(int priority, GSourceFunc function_, void* data, GDestroyNotify notify)
195 	{
196 		return g_idle_add_full(priority, function_, 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 	 * Returns: %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 	 * Returns: 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, true);
233 	}
234 }