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 = Timeout 29 * strct = 30 * realStrct= 31 * ctorStrct= 32 * clss = Timeout 33 * interf = 34 * class Code: Yes 35 * interface Code: No 36 * template for: 37 * extend = 38 * implements: 39 * prefixes: 40 * - gtk_timeout_ 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.Timeout; 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 Timeout 94 { 95 96 /** Holds all timeout delegates */ 97 bool delegate()[] timeoutListeners; 98 /** our gtk timeout ID */ 99 uint timeoutID; 100 101 102 /** 103 * Creates a new timeout cycle. 104 * Params: 105 * interval = the timeout in milieconds 106 * delegate() = the delegate to be executed 107 * fireNow = When true the delegate will be executed emmidiatly 108 */ 109 this(uint interval, bool delegate() dlg, bool fireNow=false) 110 { 111 timeoutListeners ~= dlg; 112 timeoutID = gtk_timeout_add(interval, cast(GtkFunction)&timeoutCallback, cast(void*)this); 113 if ( fireNow ) 114 { 115 if ( !dlg() ) 116 { 117 timeoutListeners.length = 0; 118 } 119 } 120 } 121 122 /** */ 123 public void stop() 124 { 125 if ( timeoutID > 0 ) 126 { 127 gtk_timeout_remove(timeoutID); 128 } 129 timeoutListeners.length = 0; 130 } 131 132 /** 133 * Removes the timeout from gtk 134 */ 135 ~this() 136 { 137 stop(); 138 } 139 140 /** 141 * Adds a new delegate to this timeout cycle 142 * Params: 143 * dlg = 144 * fireNow = 145 */ 146 public void addListener(bool delegate() dlg, bool fireNow=false) 147 { 148 timeoutListeners ~= dlg; 149 if ( fireNow ) 150 { 151 if ( !dlg() ) 152 { 153 timeoutListeners.length = timeoutListeners.length - 1; 154 } 155 } 156 } 157 158 /** 159 * The callback execution from glib 160 * Params: 161 * timeout = 162 * Returns: 163 */ 164 extern(C) static bool timeoutCallback(Timeout timeout) 165 { 166 return timeout.callAllListeners(); 167 } 168 169 /** 170 * Executes all delegates on the execution list 171 * Returns: 172 */ 173 private bool callAllListeners() 174 { 175 bool runAgain = false; 176 177 int i = 0; 178 179 while ( i<timeoutListeners.length ) 180 { 181 if ( !timeoutListeners[i]() ) 182 { 183 timeoutListeners = timeoutListeners[0..i] ~ timeoutListeners[i+1..timeoutListeners.length]; 184 } 185 else 186 { 187 runAgain = true; 188 ++i; 189 } 190 } 191 return runAgain; 192 } 193 194 /** 195 */ 196 197 /** 198 * Warning 199 * 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. 200 * Registers a function to be called periodically. The function will be called 201 * repeatedly after interval milliseconds until it returns FALSE at which 202 * point the timeout is destroyed and will not be called again. 203 * Params: 204 * interval = The time between calls to the function, in milliseconds 205 * (1/1000ths of a second.) 206 * marshal = The marshaller to use instead of the function (if non-NULL). 207 * data = The data to pass to the function. 208 * destroy = Function to call when the timeout is destroyed or NULL. 209 * Returns: A unique id for the event source. 210 */ 211 public static uint addFull(uint interval, GtkFunction funct, GtkCallbackMarshal marshal, void* data, GDestroyNotify destroy) 212 { 213 // guint gtk_timeout_add_full (guint32 interval, GtkFunction function, GtkCallbackMarshal marshal, gpointer data, GDestroyNotify destroy); 214 return gtk_timeout_add_full(interval, funct, marshal, data, destroy); 215 } 216 217 /** 218 * Warning 219 * gtk_timeout_add has been deprecated since version 2.4 and should not be used in newly-written code. Use g_timeout_add() instead. 220 * Registers a function to be called periodically. The function will be called 221 * repeatedly after interval milliseconds until it returns FALSE at which 222 * point the timeout is destroyed and will not be called again. 223 * Params: 224 * interval = The time between calls to the function, in milliseconds 225 * (1/1000ths of a second.) 226 * data = The data to pass to the function. 227 * Returns: A unique id for the event source. 228 */ 229 public static uint add(uint interval, GtkFunction funct, void* data) 230 { 231 // guint gtk_timeout_add (guint32 interval, GtkFunction function, gpointer data); 232 return gtk_timeout_add(interval, funct, data); 233 } 234 235 /** 236 * Warning 237 * gtk_timeout_remove has been deprecated since version 2.4 and should not be used in newly-written code. Use g_source_remove() instead. 238 * Removes the given timeout destroying all information about it. 239 * Params: 240 * timeoutHandlerId = The identifier returned when installing the timeout. 241 */ 242 public static void remove(uint timeoutHandlerId) 243 { 244 // void gtk_timeout_remove (guint timeout_handler_id); 245 gtk_timeout_remove(timeoutHandlerId); 246 } 247 }