2002-08-31 22:37:52 +02:00
|
|
|
// GLib.Idle.cs - Idle class implementation
|
|
|
|
//
|
|
|
|
// Author: Mike Kestner <mkestner@speakeasy.net>
|
|
|
|
// Rachel Hestilow <hestilow@ximian.com>
|
|
|
|
//
|
2004-06-25 20:42:19 +02:00
|
|
|
// Copyright (c) 2002 Mike Kestner
|
|
|
|
// Copyright (c) Rachel Hestilow
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of version 2 of the Lesser GNU General
|
|
|
|
// Public License as published by the Free Software Foundation.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this program; if not, write to the
|
|
|
|
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
// Boston, MA 02111-1307, USA.
|
|
|
|
|
2002-08-31 22:37:52 +02:00
|
|
|
|
|
|
|
namespace GLib {
|
|
|
|
|
|
|
|
using System;
|
2005-02-25 19:18:10 +01:00
|
|
|
using System.Collections;
|
2002-08-31 22:37:52 +02:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
public delegate bool IdleHandler ();
|
|
|
|
|
|
|
|
public class Idle {
|
|
|
|
|
2004-09-20 18:43:16 +02:00
|
|
|
internal class IdleProxy : SourceProxy {
|
|
|
|
public IdleProxy (IdleHandler real)
|
|
|
|
{
|
|
|
|
real_handler = real;
|
|
|
|
proxy_handler = new IdleHandler (Handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Handler ()
|
|
|
|
{
|
|
|
|
IdleHandler idle_handler = (IdleHandler) real_handler;
|
|
|
|
|
|
|
|
bool cont = idle_handler ();
|
|
|
|
if (!cont)
|
|
|
|
Remove ();
|
|
|
|
return cont;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-15 22:02:25 +01:00
|
|
|
private Idle ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-02-22 05:34:56 +01:00
|
|
|
[DllImport("libglib-2.0-0.dll")]
|
2002-08-31 22:37:52 +02:00
|
|
|
static extern uint g_idle_add (IdleHandler d, IntPtr data);
|
|
|
|
|
|
|
|
public static uint Add (IdleHandler hndlr)
|
|
|
|
{
|
2004-09-20 18:43:16 +02:00
|
|
|
IdleProxy p = new IdleProxy (hndlr);
|
|
|
|
uint code = g_idle_add ((IdleHandler) p.proxy_handler, IntPtr.Zero);
|
|
|
|
Source.source_handlers [code] = p;
|
|
|
|
|
|
|
|
return code;
|
2002-08-31 22:37:52 +02:00
|
|
|
}
|
2003-12-24 20:37:41 +01:00
|
|
|
|
|
|
|
[DllImport("libglib-2.0-0.dll")]
|
2005-02-25 19:18:10 +01:00
|
|
|
static extern bool g_source_remove_by_funcs_user_data (Delegate d, IntPtr data);
|
2003-12-24 20:37:41 +01:00
|
|
|
|
|
|
|
public static bool Remove (IdleHandler hndlr)
|
|
|
|
{
|
2005-02-25 19:18:10 +01:00
|
|
|
bool result = false;
|
|
|
|
ArrayList keys = new ArrayList ();
|
|
|
|
|
2004-09-27 02:28:16 +02:00
|
|
|
foreach (uint code in Source.source_handlers.Keys){
|
2005-02-25 19:18:10 +01:00
|
|
|
IdleProxy p = Source.source_handlers [code] as IdleProxy;
|
2004-09-20 18:43:16 +02:00
|
|
|
|
2005-02-25 19:18:10 +01:00
|
|
|
if (p != null && p.real_handler == hndlr) {
|
|
|
|
keys.Add (code);
|
|
|
|
result = g_source_remove_by_funcs_user_data (p.proxy_handler, IntPtr.Zero);
|
|
|
|
}
|
2004-09-20 18:43:16 +02:00
|
|
|
}
|
2005-02-25 19:18:10 +01:00
|
|
|
|
|
|
|
foreach (object key in keys)
|
|
|
|
Source.source_handlers.Remove (key);
|
|
|
|
|
|
|
|
return result;
|
2003-12-24 20:37:41 +01:00
|
|
|
}
|
2002-08-31 22:37:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|