Revision: 5452
          http://skim-app.svn.sourceforge.net/skim-app/?rev=5452&view=rev
Author:   hofman
Date:     2009-06-24 16:48:38 +0000 (Wed, 24 Jun 2009)

Log Message:
-----------
Revert revision 5434. Use NSEditor and NSEditorRegistration for edit commits, 
in particular before saves. Avoid problems by making sure that edits are either 
committed or discarded before the window closes and before the document is set 
to nil.

Revision Links:
--------------
    http://skim-app.svn.sourceforge.net/skim-app/?rev=5434&view=rev

Modified Paths:
--------------
    trunk/SKMainWindowController_UI.m
    trunk/SKNoteWindowController.h
    trunk/SKNoteWindowController.m
    trunk/SKPDFDocument.h
    trunk/SKPDFDocument.m
    trunk/SKPDFView.h
    trunk/SKPDFView.m

Modified: trunk/SKMainWindowController_UI.m
===================================================================
--- trunk/SKMainWindowController_UI.m   2009-06-24 16:17:17 UTC (rev 5451)
+++ trunk/SKMainWindowController_UI.m   2009-06-24 16:48:38 UTC (rev 5452)
@@ -1077,6 +1077,50 @@
     }
 }
 
+#pragma mark NSControl delegate protocol
+
+- (void)controlTextDidBeginEditing:(NSNotification *)note {
+    if ([[note object] isEqual:noteOutlineView])
+        [[self document] objectDidBeginEditing:self];
+}
+
+- (void)controlTextDidEndEditing:(NSNotification *)note {
+    if ([[note object] isEqual:noteOutlineView])
+        [[self document] objectDidEndEditing:self];
+}
+
+#pragma mark NSEditor protocol
+
+- (void)discardEditing {
+    int row = [noteOutlineView editedRow];
+    if (row != -1) {
+        id item = [noteOutlineView itemAtRow:row];
+        [[noteOutlineView currentEditor] setString:[item string]];
+        [[noteOutlineView window] makeFirstResponder:noteOutlineView];
+    }
+}
+
+- (BOOL)commitEditing {
+    if ([noteOutlineView editedRow] != -1)
+        return [[noteOutlineView window] makeFirstResponder:noteOutlineView];
+    else
+        return YES;
+}
+
+- (void)commitEditingWithDelegate:(id)delegate 
didCommitSelector:(SEL)didCommitSelector contextInfo:(void *)contextInfo {
+    BOOL didCommit = [self commitEditing];
+    if (delegate && didCommitSelector) {
+        // - (void)editor:(id)editor didCommit:(BOOL)didCommit 
contextInfo:(void *)contextInfo
+        NSInvocation *invocation = [NSInvocation 
invocationWithMethodSignature:[delegate 
methodSignatureForSelector:didCommitSelector]];
+        [invocation setTarget:delegate];
+        [invocation setSelector:didCommitSelector];
+        [invocation setArgument:&self atIndex:2];
+        [invocation setArgument:&didCommit atIndex:3];
+        [invocation setArgument:&contextInfo atIndex:4];
+        [invocation invoke];
+    }
+}
+
 #pragma mark SKPDFView delegate protocol
 
 - (void)PDFView:(PDFView *)sender editAnnotation:(PDFAnnotation *)annotation {
@@ -1522,12 +1566,6 @@
     }
 }
 
-- (void)handleDocumentWillSaveNotification:(NSNotification *)notification {
-    [pdfView endAnnotationEdit:self];
-    if ([noteOutlineView editedRow] != -1)
-        [[noteOutlineView window] makeFirstResponder:noteOutlineView];
-}
-
 - (void)handleDidChangeActiveAnnotationNotification:(NSNotification 
*)notification {
     PDFAnnotation *annotation = [pdfView activeAnnotation];
     
@@ -1648,9 +1686,6 @@
                              name:NSApplicationDidResignActiveNotification 
object:NSApp];
     [nc addObserver:self 
selector:@selector(handleApplicationWillBecomeActiveNotification:) 
                              name:NSApplicationWillBecomeActiveNotification 
object:NSApp];
-    // Document
-    [nc addObserver:self 
selector:@selector(handleDocumentWillSaveNotification:) 
-                             name:SKPDFDocumentWillSaveNotification 
object:[self document]];
     // PDFView
     [nc addObserver:self selector:@selector(handlePageChangedNotification:) 
                              name:PDFViewPageChangedNotification 
object:pdfView];

Modified: trunk/SKNoteWindowController.h
===================================================================
--- trunk/SKNoteWindowController.h      2009-06-24 16:17:17 UTC (rev 5451)
+++ trunk/SKNoteWindowController.h      2009-06-24 16:48:38 UTC (rev 5452)
@@ -71,6 +71,4 @@
 
 - (void)statusBarClicked:(id)sender;
 
-- (void)handleDocumentWillSaveNotification:(NSNotification *)notification;
-
 @end

Modified: trunk/SKNoteWindowController.m
===================================================================
--- trunk/SKNoteWindowController.m      2009-06-24 16:17:17 UTC (rev 5451)
+++ trunk/SKNoteWindowController.m      2009-06-24 16:48:38 UTC (rev 5452)
@@ -195,13 +195,15 @@
     [self updateStatusMessage];
     
     [self setWindowFrameAutosaveNameOrCascade:[self isNoteType] ? 
SKNoteWindowFrameAutosaveName : SKGenericNoteWindowFrameAutosaveName];
-    
-    [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(handleDocumentWillSaveNotification:) 
-                                                 
name:SKPDFDocumentWillSaveNotification object:[self document]];
 }
 
+- (BOOL)windowShouldClose:(id)window {
+    return [noteController commitEditing];
+}
+
 - (void)windowWillClose:(NSNotification *)aNotification {
-    [noteController commitEditing];
+    if ([noteController commitEditing] == NO)
+        [noteController discardEditing];
     if ([note respondsToSelector:@selector(setWindowIsOpen:)])
         [(PDFAnnotationText *)note setWindowIsOpen:NO];
     if ([[self window] isKeyWindow])
@@ -210,6 +212,13 @@
         [[[[self document] mainWindowController] window] makeMainWindow];
 }
 
+- (void)setDocument:(NSDocument *)document {
+    // in case the document is reset before windowWillClose: is called, I 
think this can happen on Tiger
+    if ([self document] && document == nil && [noteController commitEditing] 
== NO)
+        [noteController discardEditing];
+    [super setDocument:document];
+}
+
 - (IBAction)showWindow:(id)sender {
     [super showWindow:sender];
     if ([note respondsToSelector:@selector(setWindowIsOpen:)])
@@ -269,16 +278,22 @@
     [pdfView setActiveAnnotation:note];
 }
 
-- (void)handleDocumentWillSaveNotification:(NSNotification *)notification {
-    [noteController commitEditing];
-}
-
 - (NSUndoManager *)undoManagerForTextView:(NSTextView *)aTextView {
     if (textViewUndoManager == nil)
         textViewUndoManager = [[NSUndoManager alloc] init];
     return textViewUndoManager;
 }
 
+#pragma mark NSEditorRegistration protocol
+
+- (void)objectDidBeginEditing:(id)editor {
+    [[self document] objectDidBeginEditing:editor];
+}
+
+- (void)objectDidEndEditing:(id)editor {
+    [[self document] objectDidEndEditing:editor];
+}
+
 #pragma mark BDSKDragImageView delegate protocol
 
 - (BOOL)dragImageView:(BDSKDragImageView *)view 
writeDataToPasteboard:(NSPasteboard *)pasteboard {

Modified: trunk/SKPDFDocument.h
===================================================================
--- trunk/SKPDFDocument.h       2009-06-24 16:17:17 UTC (rev 5451)
+++ trunk/SKPDFDocument.h       2009-06-24 16:48:38 UTC (rev 5452)
@@ -38,7 +38,6 @@
 
 #import <Cocoa/Cocoa.h>
 
-extern NSString *SKPDFDocumentWillSaveNotification;
 extern NSString *SKSkimFileDidSaveNotification;
 
 enum {

Modified: trunk/SKPDFDocument.m
===================================================================
--- trunk/SKPDFDocument.m       2009-06-24 16:17:17 UTC (rev 5451)
+++ trunk/SKPDFDocument.m       2009-06-24 16:48:38 UTC (rev 5452)
@@ -81,7 +81,6 @@
 #define OPEN_META_TAGS_KEY @"com.apple.metadata:kOMUserTags"
 #define OPEN_META_RATING_KEY @"com.apple.metadata:kOMStarRating"
 
-NSString *SKPDFDocumentWillSaveNotification = 
@"SKPDFDocumentWillSaveNotification";
 NSString *SKSkimFileDidSaveNotification = @"SKSkimFileDidSaveNotification";
 
 #define SKLastExportedTypeKey @"SKLastExportedType"
@@ -436,7 +435,6 @@
 }
 
 - (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName 
error:(NSError **)outError{
-    [[NSNotificationCenter defaultCenter] 
postNotificationName:SKPDFDocumentWillSaveNotification object:self];
     BOOL didWrite = NO;
     NSError *error = nil;
     if (SKIsEmbeddedPDFDocumentType(typeName)) {

Modified: trunk/SKPDFView.h
===================================================================
--- trunk/SKPDFView.h   2009-06-24 16:17:17 UTC (rev 5451)
+++ trunk/SKPDFView.h   2009-06-24 16:48:38 UTC (rev 5452)
@@ -193,7 +193,6 @@
 
 - (void)editActiveAnnotation:(id)sender;
 - (void)editThisAnnotation:(id)sender;
-- (void)endAnnotationEdit:(id)sender;
 
 - (void)selectNextActiveAnnotation:(id)sender;
 - (void)selectPreviousActiveAnnotation:(id)sender;

Modified: trunk/SKPDFView.m
===================================================================
--- trunk/SKPDFView.m   2009-06-24 16:17:17 UTC (rev 5451)
+++ trunk/SKPDFView.m   2009-06-24 16:48:38 UTC (rev 5452)
@@ -555,7 +555,7 @@
        if (activeAnnotation != nil) {
                [self setNeedsDisplayForAnnotation:activeAnnotation];
         if (changed && [self isEditing])
-            [self endAnnotationEdit:nil];
+            [self commitEditing];
        }
     
        // Assign.
@@ -2020,7 +2020,7 @@
     if (undoable)
         [[[self undoManager] prepareWithInvocationTarget:self] 
addAnnotation:wasAnnotation toPage:page];
     if ([self isEditing] && activeAnnotation == annotation)
-        [self endAnnotationEdit:self];
+        [self commitEditing];
        if (activeAnnotation == annotation)
                [self setActiveAnnotation:nil];
     [self setNeedsDisplayForAnnotation:wasAnnotation];
@@ -2058,7 +2058,7 @@
         return;
     
     if ([self isEditing])
-        [self endAnnotationEdit:self];
+        [self commitEditing];
     if (activeAnnotation != annotation)
         [self setActiveAnnotation:annotation];
     [self editActiveAnnotation:sender];
@@ -2068,7 +2068,7 @@
     if (nil == activeAnnotation || hideNotes)
         return;
     
-    [self endAnnotationEdit:self];
+    [self commitEditing];
     
     NSString *type = [activeAnnotation type];
     
@@ -2102,6 +2102,8 @@
         
         [self setNeedsDisplayForAnnotation:activeAnnotation];
         
+        [[[[self window] windowController] document] 
objectDidBeginEditing:self];
+        
     } else if ([activeAnnotation isEditable]) {
         
         [[SKPDFToolTipWindow sharedToolTipWindow] orderOut:self];
@@ -2113,10 +2115,25 @@
     
 }
 
-- (void)endAnnotationEdit:(id)sender {
+- (void)discardEditing {
     if (editField) {
         if ([[self window] firstResponder] == [editField currentEditor] && 
[[self window] makeFirstResponder:self] == NO)
             [[self window] endEditingFor:nil];
+        [editField removeFromSuperview];
+        [editField release];
+        editField = nil;
+        
+        if ([[activeAnnotation type] isEqualToString:SKNFreeTextString])
+            [self setNeedsDisplayForAnnotation:activeAnnotation];
+        
+        [[[[self window] windowController] document] objectDidEndEditing:self];
+    }
+}
+
+- (BOOL)commitEditing {
+    if (editField) {
+        if ([[self window] firstResponder] == [editField currentEditor] && 
[[self window] makeFirstResponder:self] == NO)
+            return NO;
         if ([[editField stringValue] isEqualToString:[activeAnnotation 
string]] == NO)
             [activeAnnotation setString:[editField stringValue]];
         [editField removeFromSuperview];
@@ -2125,14 +2142,31 @@
         
         if ([[activeAnnotation type] isEqualToString:SKNFreeTextString])
             [self setNeedsDisplayForAnnotation:activeAnnotation];
+        
+        [[[[self window] windowController] document] objectDidEndEditing:self];
     }
+    return YES;
 }
 
+- (void)commitEditingWithDelegate:(id)delegate 
didCommitSelector:(SEL)didCommitSelector contextInfo:(void *)contextInfo {
+    BOOL didCommit = [self commitEditing];
+    if (delegate && didCommitSelector) {
+        // - (void)editor:(id)editor didCommit:(BOOL)didCommit 
contextInfo:(void *)contextInfo
+        NSInvocation *invocation = [NSInvocation 
invocationWithMethodSignature:[delegate 
methodSignatureForSelector:didCommitSelector]];
+        [invocation setTarget:delegate];
+        [invocation setSelector:didCommitSelector];
+        [invocation setArgument:&self atIndex:2];
+        [invocation setArgument:&didCommit atIndex:3];
+        [invocation setArgument:&contextInfo atIndex:4];
+        [invocation invoke];
+    }
+}
+
 - (BOOL)control:(NSControl *)control textView:(NSTextView *)textView 
doCommandBySelector:(SEL)command {
     BOOL rv = NO;
     if ([control isEqual:editField]) {
         if (command == @selector(insertNewline:) || command == 
@selector(insertTab:) || command == @selector(insertBacktab:)) {
-            [self endAnnotationEdit:self];
+            [self commitEditing];
             [[self window] makeFirstResponder:self];
             rv = YES;
         }
@@ -2151,7 +2185,7 @@
     
     if (activeAnnotation) {
         if ([self isEditing])
-            [self endAnnotationEdit:self];
+            [self commitEditing];
         pageIndex = [[activeAnnotation page] pageIndex];
         i = [[[activeAnnotation page] annotations] 
indexOfObject:activeAnnotation];
     } else {
@@ -2197,7 +2231,7 @@
     
     if (activeAnnotation) {
         if ([self isEditing])
-            [self endAnnotationEdit:self];
+            [self commitEditing];
         pageIndex = [[activeAnnotation page] pageIndex];
         annotations = [[activeAnnotation page] annotations];
         i = [annotations indexOfObject:activeAnnotation];


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to