Revision: 12550 http://sourceforge.net/p/skim-app/code/12550 Author: hofman Date: 2021-11-13 18:06:18 +0000 (Sat, 13 Nov 2021) Log Message: ----------- Add class files for undo manager that can forward undo and redo actions to another undo manager, to be used for editing in a text view. Use for text note editor and note window.
Modified Paths: -------------- trunk/SKNoteWindowController.m trunk/SKTextNoteEditor.h trunk/SKTextNoteEditor.m trunk/Skim.xcodeproj/project.pbxproj Added Paths: ----------- trunk/SKTextUndoManager.h trunk/SKTextUndoManager.m Modified: trunk/SKNoteWindowController.m =================================================================== --- trunk/SKNoteWindowController.m 2021-11-13 17:50:00 UTC (rev 12549) +++ trunk/SKNoteWindowController.m 2021-11-13 18:06:18 UTC (rev 12550) @@ -62,6 +62,7 @@ #import "NSView_SKExtensions.h" #import "NSPasteboard_SKExtensions.h" #import "NSAttributedString_SKExtensions.h" +#import "SKTextUndoManager.h" #define EM_DASH_CHARACTER (unichar)0x2014 @@ -342,7 +343,7 @@ - (NSUndoManager *)undoManagerForTextView:(NSTextView *)aTextView { if (textViewUndoManager == nil) - textViewUndoManager = [[NSUndoManager alloc] init]; + textViewUndoManager = [[SKTextUndoManager alloc] initWithNextUndoManager:[[self document] undoManager]]; return textViewUndoManager; } Modified: trunk/SKTextNoteEditor.h =================================================================== --- trunk/SKTextNoteEditor.h 2021-11-13 17:50:00 UTC (rev 12549) +++ trunk/SKTextNoteEditor.h 2021-11-13 18:06:18 UTC (rev 12550) @@ -44,6 +44,7 @@ NSTextView *textView; PDFView *pdfView; PDFAnnotationFreeText *annotation; + NSUndoManager *undoManager; } - (id)initWithPDFView:(PDFView *)aPDFView annotation:(PDFAnnotationFreeText *)anAnnotation; Modified: trunk/SKTextNoteEditor.m =================================================================== --- trunk/SKTextNoteEditor.m 2021-11-13 17:50:00 UTC (rev 12549) +++ trunk/SKTextNoteEditor.m 2021-11-13 18:06:18 UTC (rev 12550) @@ -43,6 +43,7 @@ #import "NSView_SKExtensions.h" #import "NSGraphics_SKExtensions.h" #import "NSEvent_SKExtensions.h" +#import "SKTextUndoManager.h" #import <SkimNotes/SkimNotes.h> static char SKPDFAnnotationPropertiesObservationContext; @@ -132,6 +133,7 @@ [textView setVerticallyResizable:YES]; [textView setAutoresizingMask:NSViewWidthSizable]; [textView setUsesFontPanel:NO]; + [textView setAllowsUndo:YES]; [textView setDelegate:self]; [textView setString:[annotation string] ?: @""]; [textView setFont:[annotation font]]; @@ -234,6 +236,12 @@ [self endEditingWithCommit:YES]; } +- (NSUndoManager *)undoManagerForTextView:(NSTextView *)view { + if (undoManager == nil) + undoManager = [[SKTextUndoManager alloc] initWithNextUndoManager:[pdfView undoManager]]; + return undoManager; +} + - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (context == &SKPDFAnnotationPropertiesObservationContext) { if ([keyPath isEqualToString:SKNPDFAnnotationBoundsKey]) { @@ -247,7 +255,12 @@ [textView setAlignment:[annotation alignment]]; [self updateParagraphStyle]; } else if ([keyPath isEqualToString:SKNPDFAnnotationStringKey]) { - [textView setString:[annotation string] ?: @""]; + NSString *string = [annotation string] ?: @""; + if ([string isEqualToString:[textView string] ?: @""] == NO) { + [textView setString:string]; + // the local undo stack is invalid now, and doing it undo safe leads to a weird stack + [undoManager removeAllActions]; + } } else if ([keyPath isEqualToString:SKNPDFAnnotationColorKey] || [keyPath isEqualToString:SKNPDFAnnotationBorderKey]) { [self setNeedsDisplay:YES]; } Added: trunk/SKTextUndoManager.h =================================================================== --- trunk/SKTextUndoManager.h (rev 0) +++ trunk/SKTextUndoManager.h 2021-11-13 18:06:18 UTC (rev 12550) @@ -0,0 +1,46 @@ +// +// SKTextUndoManager.h +// Skim +// +// Created by Christiaan Hofman on 13/11/2021. +/* +This software is Copyright (c) 2021 +Christiaan Hofman. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +- Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +- Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + +- Neither the name of Christiaan Hofman nor the names of any + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#import <Cocoa/Cocoa.h> + + +@interface SKTextUndoManager : NSUndoManager { + NSUndoManager *nextUndoManager; +} +- (id)initWithNextUndoManager:(NSUndoManager *)undoManager; +@end Added: trunk/SKTextUndoManager.m =================================================================== --- trunk/SKTextUndoManager.m (rev 0) +++ trunk/SKTextUndoManager.m 2021-11-13 18:06:18 UTC (rev 12550) @@ -0,0 +1,87 @@ +// +// SKTextUndoManager.m +// Skim +// +// Created by Christiaan Hofman on 13/11/2021. +/* +This software is Copyright (c) 2021 +Christiaan Hofman. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +- Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +- Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + +- Neither the name of Christiaan Hofman nor the names of any + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#import "SKTextUndoManager.h" + + +@implementation SKTextUndoManager + +- (id)initWithNextUndoManager:(NSUndoManager *)undoManager { + self = [super init]; + if (self) { + nextUndoManager = [undoManager retain]; + } + return self; +} + +- (void)dealloc { + SKDESTROY(nextUndoManager); + [super dealloc]; +} + +- (NSString *)redoMenuItemTitle { + return [super canRedo] ? [super redoMenuItemTitle] : [nextUndoManager redoMenuItemTitle]; +} + +- (NSString *)undoMenuItemTitle { + return [super canUndo] ? [super undoMenuItemTitle] : [nextUndoManager undoMenuItemTitle]; +} + +- (BOOL)canRedo { + return [super canRedo] || [nextUndoManager canRedo]; +} + +- (BOOL)canUndo { + return [super canUndo] || [nextUndoManager canUndo]; +} + +- (void)redo { + if ([super canRedo]) + [super redo]; + else + [nextUndoManager redo]; +} + +- (void)undo { + if ([super canUndo]) + [super undo]; + else + [nextUndoManager undo]; +} + +@end Modified: trunk/Skim.xcodeproj/project.pbxproj =================================================================== --- trunk/Skim.xcodeproj/project.pbxproj 2021-11-13 17:50:00 UTC (rev 12549) +++ trunk/Skim.xcodeproj/project.pbxproj 2021-11-13 18:06:18 UTC (rev 12550) @@ -158,6 +158,7 @@ CE5BEA190C7C635400EBDCF7 /* NSGeometry_SKExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = CE5BEA170C7C635400EBDCF7 /* NSGeometry_SKExtensions.m */; }; CE5BF8010C7CBF6300EBDCF7 /* SKTableView.m in Sources */ = {isa = PBXBuildFile; fileRef = CE5BF7FF0C7CBF6300EBDCF7 /* SKTableView.m */; }; CE5BF8430C7CC24A00EBDCF7 /* SKOutlineView.m in Sources */ = {isa = PBXBuildFile; fileRef = CE5BF8410C7CC24A00EBDCF7 /* SKOutlineView.m */; }; + CE5CB23F2740329E00315060 /* SKTextUndoManager.m in Sources */ = {isa = PBXBuildFile; fileRef = CE5CB23D2740329E00315060 /* SKTextUndoManager.m */; }; CE5F43730BF8A3410069D89C /* IOKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CE5F42D30BF8A3400069D89C /* IOKit.framework */; }; CE5FA1680C909886008BE480 /* SKFDFParser.m in Sources */ = {isa = PBXBuildFile; fileRef = CE5FA1660C909886008BE480 /* SKFDFParser.m */; }; CE61008C2624FF85007CEC88 /* NotesDocument.xib in Resources */ = {isa = PBXBuildFile; fileRef = CE61008B2624FF84007CEC88 /* NotesDocument.xib */; }; @@ -940,6 +941,8 @@ CE5BF7FF0C7CBF6300EBDCF7 /* SKTableView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SKTableView.m; sourceTree = "<group>"; }; CE5BF8400C7CC24A00EBDCF7 /* SKOutlineView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SKOutlineView.h; sourceTree = "<group>"; }; CE5BF8410C7CC24A00EBDCF7 /* SKOutlineView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SKOutlineView.m; sourceTree = "<group>"; }; + CE5CB23C2740329E00315060 /* SKTextUndoManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SKTextUndoManager.h; sourceTree = "<group>"; }; + CE5CB23D2740329E00315060 /* SKTextUndoManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SKTextUndoManager.m; sourceTree = "<group>"; }; CE5F23571057B75000B5A7B9 /* SKCompatibility.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SKCompatibility.h; sourceTree = "<group>"; }; CE5F42D30BF8A3400069D89C /* IOKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IOKit.framework; path = /System/Library/Frameworks/IOKit.framework; sourceTree = "<absolute>"; }; CE5FA1650C909886008BE480 /* SKFDFParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SKFDFParser.h; sourceTree = "<group>"; }; @@ -2013,6 +2016,8 @@ CE4972890BDE8B2900D7F1D2 /* SKToolbarItem.m */, CE5BD6710C7ADF1500EBDCF7 /* SKTypeSelectHelper.h */, CE5BD6720C7ADF1500EBDCF7 /* SKTypeSelectHelper.m */, + CE5CB23C2740329E00315060 /* SKTextUndoManager.h */, + CE5CB23D2740329E00315060 /* SKTextUndoManager.m */, ); name = "View Helpers"; sourceTree = "<group>"; @@ -2723,6 +2728,7 @@ CEE54D9C0DA2E7B30037169F /* PDFAnnotationLine_SKExtensions.m in Sources */, CEE54DAE0DA2E8CD0037169F /* PDFAnnotationFreeText_SKExtensions.m in Sources */, CEE54DB40DA2E99D0037169F /* PDFAnnotationMarkup_SKExtensions.m in Sources */, + CE5CB23F2740329E00315060 /* SKTextUndoManager.m in Sources */, CEE54F6C0DA3FB060037169F /* SKMainToolbarController.m in Sources */, CEE54F700DA3FBE00037169F /* NSSegmentedControl_SKExtensions.m in Sources */, CEE54F900DA3FE9A0037169F /* NSValueTransformer_SKExtensions.m in Sources */, This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. _______________________________________________ Skim-app-commit mailing list Skim-app-commit@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/skim-app-commit