#include <Foundation/NSAutoreleasePool.h>

%%{
%%    id delegate;		// delegate of this window object
%%@private
%%    guint handler_id;		// /delete_event/ signal handler id
%%}
%%
static gint focus_in_window (GtkWidget *win, GdkEvent *event, GTKWindow *self)
{
    NSAutoreleasePool *pool = [NSAutoreleasePool new];

    [[NSNotificationCenter defaultCenter] postNotificationName:
	GTKWindowDidBecomeMainNotification object:self];
    [pool release];
    return NO;
}

static gint focus_out_window (GtkWidget *win, GdkEvent *event, GTKWindow *self)
{
    NSAutoreleasePool *pool = [NSAutoreleasePool new];

    [[NSNotificationCenter defaultCenter] postNotificationName:
	GTKWindowDidResignMainNotification object:self];
    [pool release];
    return NO;
}

static gint delete_window (GtkWidget *win, GdkEvent *event, GTKWindow *self)
{
    NSAutoreleasePool *pool = [NSAutoreleasePool new];
    id delegate = [self delegate];
    gint result = NO;

    if ([delegate respondsToSelector:@selector(windowShouldClose:)] &&
	[delegate windowShouldClose:self] == NO) result = YES;
    [pool release];
    return result;
}

static void destroy_window (GtkWidget *win, GTKWindow *self)
{
    NSAutoreleasePool *pool = [NSAutoreleasePool new];

    [[NSNotificationCenter defaultCenter] postNotificationName:
	GTKWindowWillCloseNotification object:self];
    [pool release];
}

 - initWithGtk:(gpointer) _gtk
{
    [super initWithGtk:_gtk];
    [self ref];		// first reference is missing (due to floating flag)

    gtk_signal_connect(_gtk, "focus_in_event",
			(GtkSignalFunc) focus_in_window, self);
    gtk_signal_connect(_gtk, "focus_out_event",
			(GtkSignalFunc) focus_out_window, self);
    gtk_signal_connect(_gtk, "destroy", (GtkSignalFunc) destroy_window, self);

    [[NSNotificationCenter defaultCenter] postNotificationName:
	GTKWindowWillOpenNotification object:self];

    return  self;
}

/*
 * Set the delegate object of this window or unset it (if /delegate/
 * is |nil|). If the delegate implements some of the methods described
 * in the protocol @proto(GTKWindowDelegate), it will be notified by
 * the window on the corresponding events.
 */
- (void) setDelegate:_delegate
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

    if (delegate) [center removeObserver:delegate name:nil object:self];
    delegate = _delegate;

    if ([delegate respondsToSelector:@selector(windowDidBecomeMain:)])
	[center addObserver:delegate selector:@selector(windowDidBecomeMain:)
		name:GTKWindowDidBecomeMainNotification object:self];
    if ([delegate respondsToSelector:@selector(windowDidResignMain:)])
	[center addObserver:delegate selector:@selector(windowDidResignMain:)
		name:GTKWindowDidResignMainNotification object:self];
    if ([delegate respondsToSelector:@selector(windowWillClose:)])
	[center addObserver:delegate selector:@selector(windowWillClose:)
		name:GTKWindowWillCloseNotification object:self];

    if (handler_id == 0 &&
	[delegate respondsToSelector:@selector(windowShouldClose:)])
	handler_id = gtk_signal_connect(gtk, "delete_event",
					(GtkSignalFunc) delete_window, self);
}

/*
 * Return this window's delegate object.
 */
- delegate
{
    return delegate;
}

#define gtk	((GtkWindow *) gtk)
- (NSString *) title
{
    return String_to_NSString(gtk->title);
}
- (GTKWidget *) focusWidget
{
    return GTK_GET_OBJECT(gtk->focus_widget);
}
- (GTKWidget *) defaultWidget
{
    return GTK_GET_OBJECT(gtk->default_widget);
}
#undef gtk
%
%extern NSString *GTKWindowDidBecomeMainNotification;
NSString *GTKWindowDidBecomeMainNotification = @"GTKWindowDidBecomeMain";
%extern NSString *GTKWindowDidResignMainNotification;
NSString *GTKWindowDidResignMainNotification = @"GTKWindowDidResignMain";
%extern NSString *GTKWindowWillCloseNotification;
NSString *GTKWindowWillCloseNotification = @"GTKWindowWillClose";
%extern NSString *GTKWindowWillOpenNotification;
NSString *GTKWindowWillOpenNotification = @"GTKWindowWillOpen";
%
%/*
% * These methods may be implemented by the delegate.
% */
%@protocol GTKWindowDelegate
%- (void) windowDidBecomeMain:(NSNotification *) event;
%- (void) windowDidResignMain:(NSNotification *) event;
%- (BOOL) windowShouldClose:(GTKWindow *) sender;
%- (void) windowWillClose:(NSNotification *) event;
%@end
