#include <Foundation/Foundation.h>
#include <AppKit/AppKit.h>
#include <GNUstepGUI/GSDisplayServer.h>

@interface MyDelegate : NSObject
- (void) applicationWillFinishLaunching: (NSNotification *)not;
@end

@implementation MyDelegate : NSObject 
- (void) applicationWillFinishLaunching: (NSNotification *)not
{
  NSMenu *menu;
  
  menu = [[NSMenu new] autorelease];
  [menu addItemWithTitle: @"Quit"  
        action: @selector (terminate:)  
        keyEquivalent: @"q"];
  [NSApp setMainMenu: menu];
}
@end

int main (int argc, const char **argv)
{ 
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  GSDisplayServer	*srv;
  NSDate *limit = [NSDate distantFuture];
  srv = GSCurrentServer();    

  [NSApplication sharedApplication];
  [NSApp setDelegate: [MyDelegate new]];

  NSWindow *win= [[NSWindow alloc] \
    initWithContentRect:NSMakeRect(0, 0, 400, 300) \
    styleMask:(NSTitledWindowMask | NSClosableWindowMask | \
        NSMiniaturizableWindowMask | NSResizableWindowMask) \
    backing:NSBackingStoreBuffered \
    defer:YES];

  NSTextField *field = [[NSTextField alloc] initWithFrame:NSMakeRect(20, 20, 360, 260)];
  [[win contentView] addSubview:field];

  //[win makeKeyAndOrderFront:nil];
  
  NSModalSession session = [NSApp beginModalSessionForWindow:win];

  for (;;)
  {
    if ([NSApp runModalSession:session] != NSRunContinuesResponse)
      break;
    // DPSPeekEvent(srv, NSAnyEventMask, limit, NSDefaultRunLoopMode);
    // usleep(10);
  }
  [NSApp endModalSession:session];

  NSLog(@"done");

  [pool release];

  //return NSApplicationMain (argc, argv);
  
  return 0;
}
