#include <gtk/gtk.h>

#import "OFObject+GLibThread.h"

int _OFObject_JubGlibThread_reference;

struct params0 {
	id object;
	SEL selector;
};

struct params1 {
	id object;
	SEL selector;
	id param;
};

struct params2 {
	id object;
	SEL selector;
	id param1;
	id param2;
};

static gboolean callback0(gpointer object)
{
	struct params0 *parm = object;
	[parm->object performSelector: parm->selector];
	[parm->object freeMemory: parm];
	return FALSE;
}

static gboolean callback1(gpointer object)
{
	struct params1 *parm = object;
	[parm->object performSelector: parm->selector
			   withObject: parm->param];
	[parm->param release];
	[parm->object freeMemory: parm];
	return FALSE;
}

static gboolean callback2(gpointer object)
{
	struct params2 *parm = object;
	[parm->object performSelector: parm->selector
			   withObject: parm->param1
			   withObject: parm->param2];
	[parm->param1 release];
	[parm->param2 release];
	[parm->object freeMemory: parm];
	return FALSE;
}

@implementation OFObject (JubGlibThread)
- (void)performSelectorOnGLibThread: (SEL)selector
{
	struct params0 *parm = [self allocMemoryWithSize: sizeof(*parm)];
	parm->object = self;
	parm->selector = selector;
	g_idle_add(callback0, parm);
}

- (void)performSelectorOnGLibThread: (SEL)selector
			 withObject: (id)object
{
	struct params1 *parm = [self allocMemoryWithSize: sizeof(*parm)];
	parm->object = self;
	parm->selector = selector;
	parm->param = [object retain];
	g_idle_add(callback1, parm);
}

- (void)performSelectorOnGLibThread: (SEL)selector
			 withObject: (id)object1
			 withObject: (id)object2
{
	struct params2 *parm = [self allocMemoryWithSize: sizeof(*parm)];
	parm->object = self;
	parm->selector = selector;
	parm->param1 = [object1 retain];
	parm->param2 = [object2 retain];
	g_idle_add(callback2, parm);
}
@end
