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 public import gtkc.glibtypes; 31 32 33 /** */ 34 public class Idle 35 { 36 /** Holds all idle delegates */ 37 bool delegate()[] idleListeners; 38 /** our idle ID */ 39 uint idleID; 40 41 /** 42 * Creates a new idle cycle. 43 * Params: 44 * interval = the idle in milieconds 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 idleListeners ~= dlg; 51 idleID = g_idle_add(cast(GSourceFunc)&idleCallback, cast(void*)this); 52 if ( fireNow ) 53 { 54 if ( !dlg() ) 55 { 56 idleListeners.length = 0; 57 } 58 } 59 } 60 61 /** 62 * Creates a new idle cycle. 63 * Params: 64 * dlg = the delegate to be executed 65 * priority = Priority for the idle function 66 * fireNow = When true the delegate will be executed emmidiatly 67 */ 68 this(bool delegate() dlg, GPriority priority, bool fireNow=false) 69 { 70 idleListeners ~= dlg; 71 idleID = g_idle_add_full(priority, cast(GSourceFunc)&idleCallback, cast(void*)this, null); 72 if ( fireNow ) 73 { 74 if ( !dlg() ) 75 { 76 idleListeners.length = 0; 77 } 78 } 79 } 80 81 /** */ 82 public void stop() 83 { 84 if ( idleID > 0 ) 85 { 86 g_source_remove(idleID); 87 } 88 idleListeners.length = 0; 89 } 90 91 /** 92 * Removes the idle from gtk 93 */ 94 ~this() 95 { 96 stop(); 97 } 98 99 /** 100 * Adds a new delegate to this idle cycle 101 * Params: 102 * dlg = 103 * fireNow = 104 */ 105 public void addListener(bool delegate() dlg, bool fireNow=false) 106 { 107 idleListeners ~= dlg; 108 if ( fireNow ) 109 { 110 if ( !dlg() ) 111 { 112 idleListeners.length = idleListeners.length - 1; 113 } 114 } 115 } 116 117 /** 118 * The callback execution from glib 119 * Params: 120 * idle = 121 * Returns: 122 */ 123 extern(C) static bool idleCallback(Idle idle) 124 { 125 return idle.callAllListeners(); 126 } 127 128 /** 129 * Executes all delegates on the execution list 130 * Returns: 131 */ 132 private bool callAllListeners() 133 { 134 bool runAgain = false; 135 136 int i = 0; 137 138 while ( i<idleListeners.length ) 139 { 140 if ( !idleListeners[i]() ) 141 { 142 idleListeners = idleListeners[0..i] ~ idleListeners[i+1..idleListeners.length]; 143 } 144 else 145 { 146 runAgain = true; 147 ++i; 148 } 149 } 150 151 // Set idleID to 0 if all delegates are removed 152 if (idleListeners.length == 0) 153 idleID = 0; 154 155 return runAgain; 156 } 157 158 /** 159 */ 160 161 /** 162 * Adds a function to be called whenever there are no higher priority 163 * events pending to the default main loop. The function is given the 164 * default idle priority, #G_PRIORITY_DEFAULT_IDLE. If the function 165 * returns %FALSE it is automatically removed from the list of event 166 * sources and will not be called again. 167 * 168 * See [memory management of sources][mainloop-memory-management] for details 169 * on how to handle the return value and memory management of @data. 170 * 171 * This internally creates a main loop source using g_idle_source_new() 172 * and attaches it to the global #GMainContext using g_source_attach(), so 173 * the callback will be invoked in whichever thread is running that main 174 * context. You can do these steps manually if you need greater control or to 175 * use a custom main context. 176 * 177 * Params: 178 * funct = function to call 179 * data = data to pass to @function. 180 * 181 * Returns: the ID (greater than 0) of the event source. 182 */ 183 public static uint add(GSourceFunc funct, void* data) 184 { 185 return g_idle_add(funct, data); 186 } 187 188 /** 189 * Adds a function to be called whenever there are no higher priority 190 * events pending. If the function returns %FALSE it is automatically 191 * removed from the list of event sources and will not be called again. 192 * 193 * See [memory management of sources][mainloop-memory-management] for details 194 * on how to handle the return value and memory management of @data. 195 * 196 * This internally creates a main loop source using g_idle_source_new() 197 * and attaches it to the global #GMainContext using g_source_attach(), so 198 * the callback will be invoked in whichever thread is running that main 199 * context. You can do these steps manually if you need greater control or to 200 * use a custom main context. 201 * 202 * Params: 203 * priority = the priority of the idle source. Typically this will be in the 204 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE. 205 * funct = function to call 206 * data = data to pass to @function 207 * notify = function to call when the idle is removed, or %NULL 208 * 209 * Returns: the ID (greater than 0) of the event source. 210 */ 211 public static uint addFull(int priority, GSourceFunc funct, void* data, GDestroyNotify notify) 212 { 213 return g_idle_add_full(priority, funct, data, notify); 214 } 215 216 /** 217 * Removes the idle function with the given data. 218 * 219 * Params: 220 * data = the data for the idle source's callback. 221 * 222 * Returns: %TRUE if an idle source was found and removed. 223 */ 224 public static bool removeByData(void* data) 225 { 226 return g_idle_remove_by_data(data) != 0; 227 } 228 229 /** 230 * Creates a new idle source. 231 * 232 * The source will not initially be associated with any #GMainContext 233 * and must be added to one with g_source_attach() before it will be 234 * executed. Note that the default priority for idle sources is 235 * %G_PRIORITY_DEFAULT_IDLE, as compared to other sources which 236 * have a default priority of %G_PRIORITY_DEFAULT. 237 * 238 * Returns: the newly-created idle source 239 */ 240 public static Source sourceNew() 241 { 242 auto p = g_idle_source_new(); 243 244 if(p is null) 245 { 246 return null; 247 } 248 249 return new Source(cast(GSource*) p, true); 250 } 251 }