alternative to assert
[gtkD.git] / gtkD / src / gtk / Timeout.d
blobaca2238b7d3c5a97050f1b9844b1e9314bf97aa6
1 /*
2 * This file is part of gtkD.
4 * gtkD is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
9 * gtkD is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with gtkD; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // generated automatically - do not change
20 // find conversion definition on APILookup.txt
21 // implement new conversion functionalities on the wrap.utils pakage
24 * Conversion parameters:
25 * inFile =
26 * outPack = gtk
27 * outFile = Timeout
28 * strct =
29 * realStrct=
30 * ctorStrct=
31 * clss = Timeout
32 * interf =
33 * class Code: Yes
34 * interface Code: No
35 * template for:
36 * extend =
37 * implements:
38 * prefixes:
39 * - gtk_timeout_
40 * omit structs:
41 * omit prefixes:
42 * omit code:
43 * imports:
44 * structWrap:
45 * module aliases:
46 * local aliases:
49 module gtk.Timeout;
51 version(noAssert)
53 version(Tango)
55 import tango.io.Stdout; // use the tango loging?
59 private import gtkc.gtktypes;
61 private import gtkc.gtk;
68 /**
69 * Description
70 * Before using GTK+, you need to initialize it; initialization connects
71 * to the window system display, and parses some standard command line
72 * arguments. The gtk_init() function initializes GTK+. gtk_init() exits
73 * the application if errors occur; to avoid this, use gtk_init_check().
74 * gtk_init_check() allows you to recover from a failed GTK+
75 * initialization - you might start up your application in text mode instead.
76 * Like all GUI toolkits, GTK+ uses an event-driven programming
77 * model. When the user is doing nothing, GTK+ sits in the
78 * main loop and waits for input. If the user
79 * performs some action - say, a mouse click - then the main loop "wakes
80 * up" and delivers an event to GTK+. GTK+ forwards the event to one or
81 * more widgets.
82 * When widgets receive an event, they frequently emit one or more
83 * signals. Signals notify your program that
84 * "something interesting happened" by invoking functions you've
85 * connected to the signal with g_signal_connect(). Functions connected
86 * to a signal are often termed callbacks.
87 * When your callbacks are invoked, you would typically take some action
88 * - for example, when an Open button is clicked you might display a
89 * GtkFileSelectionDialog. After a callback finishes, GTK+ will return
90 * to the main loop and await more user input.
91 * Example1.Typical main function for a GTK+ application
92 * int
93 * main (int argc, char **argv)
94 * {
95 * /+* Initialize i18n support +/
96 * gtk_set_locale ();
97 * /+* Initialize the widget set +/
98 * gtk_init (argc, argv);
99 * /+* Create the main window +/
100 * mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
101 * /+* Set up our GUI elements +/
102 * ...
103 * /+* Show the application window +/
104 * gtk_widget_show_all (mainwin);
105 * /+* Enter the main event loop, and wait for user interaction +/
106 * gtk_main ();
107 * /+* The user lost interest +/
108 * return 0;
110 * It's OK to use the GLib main loop directly instead of gtk_main(),
111 * though it involves slightly more typing. See GMainLoop in the GLib
112 * documentation.
114 public class Timeout
117 /** Holds all timeout delegates */
118 bool delegate()[] timeoutListeners;
119 /** our gtk timeout ID */
120 uint timeoutID;
124 * Creates a new timeout cycle.
125 * Params:
126 * interval = the timeout in milieconds
127 * delegate() = the delegate to be executed
128 * fireNow = When true the delegate will be executed emmidiatly
129 * Returns:
131 this(uint interval, bool delegate() dlg, bool fireNow=false)
133 timeoutListeners ~= dlg;
134 timeoutID = gtk_timeout_add(interval, cast(GtkFunction)&timeoutCallback, cast(void*)this);
135 if ( fireNow )
137 if ( !dlg() )
139 timeoutListeners.length = 0;
144 public void stop()
146 if ( timeoutID > 0 )
148 gtk_timeout_remove(timeoutID);
150 timeoutListeners.length = 0;
154 * Removes the timeout from gtk
155 * Returns:
157 ~this()
159 stop();
163 * Adds a new delegate to this timeout cycle
164 * Params:
165 * delegate() =
166 * fireNow =
168 public void addListener(bool delegate() dlg, bool fireNow=false)
170 timeoutListeners ~= dlg;
171 if ( fireNow )
173 if ( !dlg() )
175 timeoutListeners.length = timeoutListeners.length - 1;
181 * The callback execution from glib
182 * Params:
183 * timeout =
184 * Returns:
186 extern(C) static bool timeoutCallback(Timeout timeout)
188 return timeout.callAllListeners();
192 * Executes all delegates on the execution list
193 * Returns:
195 private bool callAllListeners()
197 bool runAgain = false;
199 int i = 0;
201 while ( i<timeoutListeners.length )
203 if ( !timeoutListeners[i]() )
205 timeoutListeners = timeoutListeners[0..i] ~ timeoutListeners[i+1..timeoutListeners.length];
207 else
209 runAgain = true;
210 ++i;
213 return runAgain;
249 * Warning
250 * gtk_timeout_add_full has been deprecated since version 2.4 and should not be used in newly-written code. Use g_timeout_add_full() instead.
251 * Registers a function to be called periodically. The function will be called
252 * repeatedly after interval milliseconds until it returns FALSE at which
253 * point the timeout is destroyed and will not be called again.
254 * interval:
255 * The time between calls to the function, in milliseconds
256 * (1/1000ths of a second.)
257 * function:
258 * The function to call periodically.
259 * marshal:
260 * The marshaller to use instead of the function (if non-NULL).
261 * data:
262 * The data to pass to the function.
263 * destroy:
264 * Function to call when the timeout is destroyed or NULL.
265 * Returns:
266 * A unique id for the event source.
268 public static uint addFull(uint interval, GtkFunction funct, GtkCallbackMarshal marshal, void* data, GtkDestroyNotify destroy)
270 // guint gtk_timeout_add_full (guint32 interval, GtkFunction function, GtkCallbackMarshal marshal, gpointer data, GtkDestroyNotify destroy);
271 return gtk_timeout_add_full(interval, funct, marshal, data, destroy);
275 * Warning
276 * gtk_timeout_add has been deprecated since version 2.4 and should not be used in newly-written code. Use g_timeout_add() instead.
277 * Registers a function to be called periodically. The function will be called
278 * repeatedly after interval milliseconds until it returns FALSE at which
279 * point the timeout is destroyed and will not be called again.
280 * interval:
281 * The time between calls to the function, in milliseconds
282 * (1/1000ths of a second.)
283 * function:
284 * The function to call periodically.
285 * data:
286 * The data to pass to the function.
287 * Returns:
288 * A unique id for the event source.
290 public static uint add(uint interval, GtkFunction funct, void* data)
292 // guint gtk_timeout_add (guint32 interval, GtkFunction function, gpointer data);
293 return gtk_timeout_add(interval, funct, data);
297 * Warning
298 * gtk_timeout_remove has been deprecated since version 2.4 and should not be used in newly-written code. Use g_source_remove() instead.
299 * Removes the given timeout destroying all information about it.
300 * timeout_handler_id:
301 * The identifier returned when installing the timeout.
303 public static void remove(uint timeoutHandlerId)
305 // void gtk_timeout_remove (guint timeout_handler_id);
306 gtk_timeout_remove(timeoutHandlerId);