Index: NSApplication.m
===================================================================
RCS file: /cvsroot/gnustep/gnustep/core/gui/Source/NSApplication.m,v
retrieving revision 1.249
diff -u -r1.249 NSApplication.m
--- NSApplication.m	1 Sep 2003 18:17:51 -0000	1.249
+++ NSApplication.m	16 Sep 2003 07:26:24 -0000
@@ -1649,6 +1649,18 @@
 	    }
 	  resp = [resp nextResponder];
 	}
+
+      if ([NSDocumentController isDocumentBasedApplication])
+        {
+          resp = [[NSDocumentController sharedDocumentController]
+                   documentForWindow: keyWindow];
+
+          if (resp != nil  && [resp respondsToSelector: aSelector])
+            {
+              return resp;
+	    }
+        }
+	
       if ([keyWindow respondsToSelector: aSelector])
 	{
 	  return keyWindow;
@@ -1658,17 +1670,6 @@
       if (resp != nil && [resp respondsToSelector: aSelector])
 	{
 	  return resp;
-	}
-
-      if ([NSDocumentController isDocumentBasedApplication])
-	{
-	  resp = [[NSDocumentController sharedDocumentController]
-		   documentForWindow: keyWindow];
-	  
-	  if (resp != nil  && [resp respondsToSelector: aSelector])
-	    {
-	      return resp;
-	    }
 	}
     }
 
Index: NSWindow.m
===================================================================
RCS file: /cvsroot/gnustep/gnustep/core/gui/Source/NSWindow.m,v
retrieving revision 1.285
diff -u -r1.285 NSWindow.m
--- NSWindow.m	4 Sep 2003 16:47:30 -0000	1.285
+++ NSWindow.m	16 Sep 2003 07:26:28 -0000
@@ -2304,6 +2304,8 @@
 
 - (void) performClose: (id)sender
 {
+  NSDocument *document = [_windowController document];
+      
   /* Don't close if a modal session is running and we are not the
      modal window */
   if ([NSApp modalWindow] && self != [NSApp modalWindow])
@@ -2318,8 +2320,6 @@
 
   if (_windowController)
     {
-      NSDocument *document = [_windowController document];
-
       if (document && ![document shouldCloseWindowController: _windowController])
         {
           NSBeep();
@@ -2355,7 +2355,14 @@
     }
 
   // FIXME: The button should be highlighted
-  [self close];
+  if (document)
+    {
+      [document close];
+    }
+  else 
+    {
+      [self close];
+    }
 }
 
 - (BOOL) performKeyEquivalent: (NSEvent*)theEvent
