<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -39,6 +39,11 @@ CPApp = nil;
 
 CPApplicationWillFinishLaunchingNotification    = @&quot;CPApplicationWillFinishLaunchingNotification&quot;;
 CPApplicationDidFinishLaunchingNotification     = @&quot;CPApplicationDidFinishLaunchingNotification&quot;;
+CPApplicationWillTerminateNotification          = @&quot;CPApplicationWillTerminateNotification&quot;;
+
+CPTerminateNow      = YES;
+CPTerminateCancel   = NO;
+CPTerminateLater    = -1; // not currently supported
 
 CPRunStoppedResponse    = -1000;
 CPRunAbortedResponse    = -1001;
@@ -295,7 +300,30 @@ CPRunContinuesResponse  = -1002;
 
 - (void)terminate:(id)aSender
 {
-    [CPPlatform terminateApplication];
+    [[CPDocumentController sharedDocumentController] closeAllDocumentsWithDelegate:self
+                                                              didCloseAllSelector:@selector(_documentController:didCloseAll:context:)
+                                                                      contextInfo:nil];
+}
+
+- (void)_documentController:(NSDocumentController *)docController didCloseAll:(BOOL)didCloseAll context:(Object)info
+{
+    // callback method for terminate:
+    if (didCloseAll)
+    {
+        if ([_delegate respondsToSelector:@selector(applicationShouldTerminate:)])
+            [self replyToApplicationShouldTerminate:[_delegate applicationShouldTerminate:self]];
+        else
+            [self replyToApplicationShouldTerminate:YES];
+    }
+}
+
+- (void)replyToApplicationShouldTerminate:(BOOL)terminate
+{
+    if (terminate == CPTerminateNow)
+    {
+        [[CPNotificationCenter defaultCenter] postNotificationName:CPApplicationWillTerminateNotification object:self];
+        [CPPlatform terminateApplication];
+    }
 }
 
 - (void)activateIgnoringOtherApps:(BOOL)shouldIgnoreOtherApps</diff>
      <filename>AppKit/CPApplication.j</filename>
    </modified>
    <modified>
      <diff>@@ -323,3 +323,38 @@ var CPSharedDocumentController = nil;
 }
 
 @end
+
+@implementation CPDocumentController (Closing)
+
+- (void)closeAllDocumentsWithDelegate:(id)aDelegate didCloseAllSelector:(SEL)didCloseSelector contextInfo:(Object)info 
+{
+    var context = {
+        delegate: aDelegate,
+        selector: didCloseSelector,
+        context: info
+    };
+
+    [self _closeDocumentsStartingWith:nil shouldClose:YES context:context];
+}
+
+// Recursive callback method. Start it by passing in a document of nil.
+- (void)_closeDocumentsStartingWith:(CPDocument)aDocument shouldClose:(BOOL)shouldClose context:(Object)context
+{
+    if (shouldClose)
+    {
+        [aDocument close];
+
+        if ([[self documents] count] &gt; 0)
+        {
+            [[[self documents] lastObject] canCloseDocumentWithDelegate:self
+                                                    shouldCloseSelector:@selector(_closeDocumentsStartingWith:shouldClose:context:)
+                                                            contextInfo:context];
+            return;
+        }
+    }
+
+    if ([context.delegate respondsToSelector:context.selector])
+        objj_msgSend(context.delegate, context.selector, self, [[self documents] count] === 0, context.context);
+}
+
+@end</diff>
      <filename>AppKit/CPDocumentController.j</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>039de1b6e8f2c7fc93b90f0029cda36d482fab86</id>
    </parent>
  </parents>
  <author>
    <name>Ross Boucher</name>
    <email>ross@280north.com</email>
  </author>
  <url>http://github.com/jsmecham/cappuccino/commit/a0a81be042b06e7bf96948991ea84fc94c49eedc</url>
  <id>a0a81be042b06e7bf96948991ea84fc94c49eedc</id>
  <committed-date>2009-10-27T18:44:09-07:00</committed-date>
  <authored-date>2009-10-27T18:44:09-07:00</authored-date>
  <message>Add support for reviewing unsaved documents before application termination.</message>
  <tree>dba4890505dfd2d4206517dcf564791b05f83cb7</tree>
  <committer>
    <name>Ross Boucher</name>
    <email>ross@280north.com</email>
  </committer>
</commit>
