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  * Conversion parameters:
26  * inFile  = 
27  * outPack = gtk
28  * outFile = Idle
29  * strct   = 
30  * realStrct=
31  * ctorStrct=
32  * clss    = Idle
33  * interf  = 
34  * class Code: Yes
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- gtk_idle_
41  * omit structs:
42  * omit prefixes:
43  * omit code:
44  * omit signals:
45  * imports:
46  * structWrap:
47  * module aliases:
48  * local aliases:
49  * overrides:
50  */
51 
52 module gtk.Idle;
53 
54 public  import gtkc.gtktypes;
55 
56 private import gtkc.gtk;
57 private import glib.ConstructionException;
58 private import gobject.ObjectG;
59 
60 
61 
62 
63 
64 
65 /**
66  * Description
67  * Before using GTK+, you need to initialize it; initialization connects
68  * to the window system display, and parses some standard command line
69  * arguments. The gtk_init() function initializes GTK+. gtk_init() exits
70  * the application if errors occur; to avoid this, use gtk_init_check().
71  * gtk_init_check() allows you to recover from a failed GTK+
72  * initialization - you might start up your application in text mode instead.
73  * Like all GUI toolkits, GTK+ uses an event-driven programming
74  * model. When the user is doing nothing, GTK+ sits in the
75  * main loop and waits for input. If the user
76  * performs some action - say, a mouse click - then the main loop "wakes
77  * up" and delivers an event to GTK+. GTK+ forwards the event to one or
78  * more widgets.
79  * When widgets receive an event, they frequently emit one or more
80  * signals. Signals notify your program that
81  * "something interesting happened" by invoking functions you've
82  * connected to the signal with g_signal_connect(). Functions connected
83  * to a signal are often termed callbacks.
84  * When your callbacks are invoked, you would typically take some action
85  * - for example, when an Open button is clicked you might display a
86  * GtkFileSelectionDialog. After a callback finishes, GTK+ will return
87  * to the main loop and await more user input.
88  * $(DDOC_COMMENT example)
89  * It's OK to use the GLib main loop directly instead of gtk_main(),
90  * though it involves slightly more typing. See GMainLoop in the GLib
91  * documentation.
92  */
93 public class Idle
94 {
95 	
96 	/** Holds all idle delegates */
97 	bool delegate()[] idleListeners;
98 	/** our gtk idle ID */
99 	uint idleID;
100 	
101 	/**
102 	 * Creates a new idle cycle.
103 	 * Params:
104 	 *    	interval = the idle in milieconds
105 	 *    	dlg = the delegate to be executed
106 	 *    	fireNow = When true the delegate will be executed emmidiatly
107 	 */
108 	this(bool delegate() dlg, bool fireNow=false)
109 	{
110 		idleListeners ~= dlg;
111 		idleID = gtk_idle_add(cast(GtkFunction)&idleCallback, cast(void*)this);
112 		if ( fireNow )
113 		{
114 			if ( !dlg() )
115 			{
116 				idleListeners.length = 0;
117 			}
118 		}
119 	}
120 	
121 	/** */
122 	public void stop()
123 	{
124 		if ( idleID > 0 )
125 		{
126 			gtk_idle_remove(idleID);
127 		}
128 		idleListeners.length = 0;
129 	}
130 	
131 	/**
132 	 * Removes the idle from gtk
133 	 */
134 	~this()
135 	{
136 		stop();
137 	}
138 	
139 	/**
140 	 * Adds a new delegate to this idle cycle
141 	 * Params:
142 	 *    	dlg =
143 	 *    	fireNow =
144 	 */
145 	public void addListener(bool delegate() dlg, bool fireNow=false)
146 	{
147 		idleListeners ~= dlg;
148 		if ( fireNow )
149 		{
150 			if ( !dlg() )
151 			{
152 				idleListeners.length = idleListeners.length - 1;
153 			}
154 		}
155 	}
156 	
157 	/**
158 	 * The callback execution from glib
159 	 * Params:
160 	 *    	idle =
161 	 * Returns:
162 	 */
163 	extern(C) static bool idleCallback(Idle idle)
164 	{
165 		return idle.callAllListeners();
166 	}
167 	
168 	/**
169 	 * Executes all delegates on the execution list
170 	 * Returns:
171 	 */
172 	private bool callAllListeners()
173 	{
174 		bool runAgain = false;
175 		
176 		int i = 0;
177 		
178 		while ( i<idleListeners.length )
179 		{
180 			if ( !idleListeners[i]() )
181 			{
182 				idleListeners = idleListeners[0..i] ~ idleListeners[i+1..idleListeners.length];
183 			}
184 			else
185 			{
186 				runAgain = true;
187 				++i;
188 			}
189 		}
190 		return runAgain;
191 	}
192 	
193 	/**
194 	 */
195 	
196 	/**
197 	 * Warning
198 	 * gtk_idle_add has been deprecated since version 2.4 and should not be used in newly-written code. Use g_idle_add() instead.
199 	 * Causes the mainloop to call the given function whenever no events with
200 	 * higher priority are to be processed. The default priority is
201 	 * GTK_PRIORITY_DEFAULT, which is rather low.
202 	 * Params:
203 	 * data = The information to pass to the function.
204 	 * Returns: a unique handle for this registration.
205 	 */
206 	public static uint add(GtkFunction funct, void* data)
207 	{
208 		// guint gtk_idle_add (GtkFunction function,  gpointer data);
209 		return gtk_idle_add(funct, data);
210 	}
211 	
212 	/**
213 	 * Warning
214 	 * gtk_idle_add_priority has been deprecated since version 2.4 and should not be used in newly-written code. Use g_idle_add_full() instead.
215 	 * Like gtk_idle_add() this function allows you to have a function called
216 	 * when the event loop is idle. The difference is that you can give a
217 	 * priority different from GTK_PRIORITY_DEFAULT to the idle function.
218 	 * Params:
219 	 * priority = The priority which should not be above G_PRIORITY_HIGH_IDLE.
220 	 * Note that you will interfere with GTK+ if you use a priority above
221 	 * GTK_PRIORITY_RESIZE.
222 	 * data = Data to pass to that function.
223 	 * Returns: A unique id for the event source.
224 	 */
225 	public static uint addPriority(int priority, GtkFunction funct, void* data)
226 	{
227 		// guint gtk_idle_add_priority (gint priority,  GtkFunction function,  gpointer data);
228 		return gtk_idle_add_priority(priority, funct, data);
229 	}
230 	
231 	/**
232 	 * Warning
233 	 * gtk_idle_add_full has been deprecated since version 2.4 and should not be used in newly-written code. Use g_idle_add_full() instead.
234 	 * Like gtk_idle_add() this function allows you to have a function called
235 	 * when the event loop is idle. The difference is that you can give a
236 	 * priority different from GTK_PRIORITY_DEFAULT to the idle function.
237 	 * Params:
238 	 * priority = The priority which should not be above G_PRIORITY_HIGH_IDLE.
239 	 * Note that you will interfere with GTK+ if you use a priority above
240 	 * GTK_PRIORITY_RESIZE.
241 	 * marshal = The marshaller to use instead of the function (if non-NULL).
242 	 * data = Data to pass to that function.
243 	 * destroy = Function to call when the timeout is destroyed or NULL.
244 	 * Returns: A unique id for the event source.
245 	 */
246 	public static uint addFull(int priority, GtkFunction funct, GtkCallbackMarshal marshal, void* data, GDestroyNotify destroy)
247 	{
248 		// guint gtk_idle_add_full (gint priority,  GtkFunction function,  GtkCallbackMarshal marshal,  gpointer data,  GDestroyNotify destroy);
249 		return gtk_idle_add_full(priority, funct, marshal, data, destroy);
250 	}
251 	
252 	/**
253 	 * Warning
254 	 * gtk_idle_remove has been deprecated since version 2.4 and should not be used in newly-written code. Use g_source_remove() instead.
255 	 * Removes the idle function with the given id.
256 	 * Params:
257 	 * idleHandlerId = Identifies the idle function to remove.
258 	 */
259 	public static void remove(uint idleHandlerId)
260 	{
261 		// void gtk_idle_remove (guint idle_handler_id);
262 		gtk_idle_remove(idleHandlerId);
263 	}
264 	
265 	/**
266 	 * Warning
267 	 * gtk_idle_remove_by_data has been deprecated since version 2.4 and should not be used in newly-written code. Use g_idle_remove_by_data() instead.
268 	 * Removes the idle function identified by the user data.
269 	 * Params:
270 	 * data = remove the idle function which was registered with this user data.
271 	 */
272 	public static void removeByData(void* data)
273 	{
274 		// void gtk_idle_remove_by_data (gpointer data);
275 		gtk_idle_remove_by_data(data);
276 	}
277 }