alternative to assert
[gtkD.git] / src / gtk / Timeout.d
blob9b5f713813838a10fc96945f901451ea54598832
1 /*
2 * This file is part of duit.
4 * duit 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 * duit 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 duit; 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 * local aliases:
48 module gtk.Timeout;
50 private import gtk.gtktypes;
52 private import lib.gtk;
55 /**
56 * Description
57 * Before using GTK+, you need to initialize it; initialization connects
58 * to the window system display, and parses some standard command line
59 * arguments. The gtk_init() function initializes GTK+. gtk_init() exits
60 * the application if errors occur; to avoid this, use gtk_init_check().
61 * gtk_init_check() allows you to recover from a failed GTK+
62 * initialization - you might start up your application in text mode instead.
63 * Like all GUI toolkits, GTK+ uses an event-driven programming
64 * model. When the user is doing nothing, GTK+ sits in the
65 * main loop and waits for input. If the user
66 * performs some action - say, a mouse click - then the main loop "wakes
67 * up" and delivers an event to GTK+. GTK+ forwards the event to one or
68 * more widgets.
69 * When widgets receive an event, they frequently emit one or more
70 * signals. Signals notify your program that
71 * "something interesting happened" by invoking functions you've
72 * connected to the signal with g_signal_connect(). Functions connected
73 * to a signal are often termed callbacks.
74 * When your callbacks are invoked, you would typically take some action
75 * - for example, when an Open button is clicked you might display a
76 * GtkFileSelectionDialog. After a callback finishes, GTK+ will return
77 * to the main loop and await more user input.
78 * Example1.Typical main function for a GTK+ application
79 * int
80 * main (int argc, char **argv)
81 * {
82 * /+* Initialize i18n support +/
83 * gtk_set_locale ();
84 * /+* Initialize the widget set +/
85 * gtk_init (argc, argv);
86 * /+* Create the main window +/
87 * mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
88 * /+* Set up our GUI elements +/
89 * ...
90 * /+* Show the application window +/
91 * gtk_widget_show_all (mainwin);
92 * /+* Enter the main event loop, and wait for user interaction +/
93 * gtk_main ();
94 * /+* The user lost interest +/
95 * return 0;
96 * }
97 * It's OK to use the GLib main loop directly instead of gtk_main(),
98 * though it involves slightly more typing. See GMainLoop in the GLib
99 * documentation.
101 public class Timeout
104 /** Holds all timeout delegates */
105 bit delegate()[] timeoutListeners;
106 /** our gtk timeout ID */
107 uint timeoutID;
111 * Creates a new timeout cycle.
112 * Params:
113 * interval = the timeout in milieconds
114 * delegate() = the delegate to be executed
115 * fireNow = When true the delegate will be executed emmidiatly
116 * Returns:
118 this(uint interval, bit delegate() dlg, bit fireNow=false)
120 timeoutListeners ~= dlg;
121 timeoutID = gtk_timeout_add(interval, cast(GtkFunction)&timeoutCallback, this);
122 if ( fireNow )
124 if ( !dlg() )
126 timeoutListeners.length = 0;
131 public void stop()
133 if ( timeoutID > 0 )
135 gtk_timeout_remove(timeoutID);
137 timeoutListeners.length = 0;
141 * Removes the timeout from gtk
142 * Returns:
144 ~this()
146 stop();
150 * Adds a new delegate to this timeout cycle
151 * Params:
152 * delegate() =
153 * fireNow =
155 public void addListener(bit delegate() dlg, bit fireNow=false)
157 timeoutListeners ~= dlg;
158 if ( fireNow )
160 if ( !dlg() )
162 timeoutListeners.length = timeoutListeners.length - 1;
168 * The callback execution from glib
169 * Params:
170 * timeout =
171 * Returns:
173 extern(C) static bit timeoutCallback(Timeout timeout)
175 return timeout.callAllListeners();
179 * Executes all delegates on the execution list
180 * Returns:
182 private bit callAllListeners()
184 bit runAgain = false;
186 int i = 0;
188 while ( i<timeoutListeners.length )
190 if ( !timeoutListeners[i]() )
192 timeoutListeners = timeoutListeners[0..i] ~ timeoutListeners[i+1..timeoutListeners.length];
194 else
196 runAgain = true;
197 ++i;
200 return runAgain;
236 * Warning
237 * 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.
238 * Registers a function to be called periodically. The function will be called
239 * repeatedly after interval milliseconds until it returns FALSE at which
240 * point the timeout is destroyed and will not be called again.
241 * interval:
242 * The time between calls to the function, in milliseconds
243 * (1/1000ths of a second.)
244 * function:
245 * The function to call periodically.
246 * marshal:
247 * The marshaller to use instead of the function (if non-NULL).
248 * data:
249 * The data to pass to the function.
250 * destroy:
251 * Function to call when the timeout is destroyed or NULL.
252 * Returns:
253 * A unique id for the event source.
255 public static uint addFull(uint interval, GtkFunction funct, GtkCallbackMarshal marshal, void* data, GtkDestroyNotify destroy)
257 // guint gtk_timeout_add_full (guint32 interval, GtkFunction function, GtkCallbackMarshal marshal, gpointer data, GtkDestroyNotify destroy);
258 return gtk_timeout_add_full(interval, funct, marshal, data, destroy);
262 * Warning
263 * gtk_timeout_add has been deprecated since version 2.4 and should not be used in newly-written code. Use g_timeout_add() instead.
264 * Registers a function to be called periodically. The function will be called
265 * repeatedly after interval milliseconds until it returns FALSE at which
266 * point the timeout is destroyed and will not be called again.
267 * interval:
268 * The time between calls to the function, in milliseconds
269 * (1/1000ths of a second.)
270 * function:
271 * The function to call periodically.
272 * data:
273 * The data to pass to the function.
274 * Returns:
275 * A unique id for the event source.
277 public static uint add(uint interval, GtkFunction funct, void* data)
279 // guint gtk_timeout_add (guint32 interval, GtkFunction function, gpointer data);
280 return gtk_timeout_add(interval, funct, data);
284 * Warning
285 * gtk_timeout_remove has been deprecated since version 2.4 and should not be used in newly-written code. Use g_source_remove() instead.
286 * Removes the given timeout destroying all information about it.
287 * timeout_handler_id:
288 * The identifier returned when installing the timeout.
290 public static void remove(uint timeoutHandlerId)
292 // void gtk_timeout_remove (guint timeout_handler_id);
293 gtk_timeout_remove(timeoutHandlerId);