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