forked from jjgod/TextEditPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Document.m
executable file
·1305 lines (1126 loc) · 68.5 KB
/
Document.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
File: Document.m
Abstract: Document object for TextEdit, a subclass of NSDocument.
Version: 1.7.1
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2012 Apple Inc. All Rights Reserved.
*/
#import <Cocoa/Cocoa.h>
#import "EncodingManager.h"
#import "Document.h"
#import "DocumentController.h"
#import "DocumentWindowController.h"
#import "PrintPanelAccessoryController.h"
#import "PrintingTextView.h"
#import "TextEditDefaultsKeys.h"
#import "TextEditErrors.h"
#import "TextEditMisc.h"
#import <objc/message.h> // objc_msgSend
#define oldEditPaddingCompensation 12.0
NSString *SimpleTextType = @"com.apple.traditional-mac-plain-text";
NSString *Word97Type = @"com.microsoft.word.doc";
NSString *Word2007Type = @"org.openxmlformats.wordprocessingml.document";
NSString *Word2003XMLType = @"com.microsoft.word.wordml";
NSString *OpenDocumentTextType = @"org.oasis-open.opendocument.text";
@implementation Document
+ (BOOL)isRichTextType:(NSString *)typeName {
/* We map all plain text documents to public.text. Therefore a document is rich iff its type is not public.text. */
return ![typeName isEqualToString:(NSString *)kUTTypeText];
}
+ (NSString *)readableTypeForType:(NSString *)type {
// There is a partial order on readableTypes given by UTTypeConformsTo. We linearly extend the partial order to a total order using <.
// Therefore we can compute the ancestor with greatest level (furthest from root) by linear search in the resulting array.
// Why do we have to do this? Because type might conform to multiple readable types, such as "public.rtf" and "public.text" and "public.data"
// and we want to find the most specialized such type.
static NSArray *topologicallySortedReadableTypes;
static dispatch_once_t pred;
dispatch_once(&pred, ^{
topologicallySortedReadableTypes = [self readableTypes];
topologicallySortedReadableTypes = [topologicallySortedReadableTypes sortedArrayUsingComparator:^(id type1, id type2) {
if (type1 == type2) return (NSComparisonResult)NSOrderedSame;
if (UTTypeConformsTo((CFStringRef)type1, (CFStringRef)type2)) return (NSComparisonResult)NSOrderedAscending;
if (UTTypeConformsTo((CFStringRef)type2, (CFStringRef)type1)) return (NSComparisonResult)NSOrderedDescending;
return (NSComparisonResult)(((NSUInteger)type1 < (NSUInteger)type2) ? (NSComparisonResult)NSOrderedAscending : (NSComparisonResult)NSOrderedDescending);
}];
[topologicallySortedReadableTypes retain];
});
for (NSString *readableType in topologicallySortedReadableTypes) {
if (UTTypeConformsTo((CFStringRef)type, (CFStringRef)readableType)) return readableType;
}
return nil;
}
- (id)init {
if ((self = [super init])) {
[[self undoManager] disableUndoRegistration];
textStorage = [[NSTextStorage allocWithZone:[self zone]] init];
[self setBackgroundColor:[NSColor whiteColor]];
[self setEncoding:NoStringEncoding];
[self setEncodingForSaving:NoStringEncoding];
[self setScaleFactor:1.0];
[self setDocumentPropertiesToDefaults];
inDuplicate = NO;
saveOperationTypeLock = [[NSLock alloc] init];
// Assume the default file type for now, since -initWithType:error: does not currently get called when creating documents using AppleScript. (4165700)
[self setFileType:[[NSDocumentController sharedDocumentController] defaultType]];
[self setPrintInfo:[self printInfo]];
hasMultiplePages = [[NSUserDefaults standardUserDefaults] boolForKey:ShowPageBreaks];
[[self undoManager] enableUndoRegistration];
}
return self;
}
/* Return an NSDictionary which maps Cocoa text system document identifiers (as declared in AppKit/NSAttributedString.h) to document types declared in TextEdit's Info.plist.
*/
- (NSDictionary *)textDocumentTypeToTextEditDocumentTypeMappingTable {
static NSDictionary *documentMappings = nil;
// Use of dispatch_once() makes the initialization thread-safe, and it needs to be, since multiple documents can be opened concurrently
static dispatch_once_t once = 0;
dispatch_once(&once, ^{
documentMappings = [[NSDictionary alloc] initWithObjectsAndKeys:
(NSString *)kUTTypeText, NSPlainTextDocumentType,
(NSString *)kUTTypeRTF, NSRTFTextDocumentType,
(NSString *)kUTTypeRTFD, NSRTFDTextDocumentType,
SimpleTextType, NSMacSimpleTextDocumentType,
(NSString *)kUTTypeHTML, NSHTMLTextDocumentType,
Word97Type, NSDocFormatTextDocumentType,
Word2007Type, NSOfficeOpenXMLTextDocumentType,
Word2003XMLType, NSWordMLTextDocumentType,
OpenDocumentTextType, NSOpenDocumentTextDocumentType,
(NSString *)kUTTypeWebArchive, NSWebArchiveTextDocumentType,
nil];
});
return documentMappings;
}
/* This method is called by the document controller. The message is passed on after information about the selected encoding (from our controller subclass) and preference regarding HTML and RTF formatting has been added. -lastSelectedEncodingForURL: returns the encoding specified in the Open panel, or the default encoding if the document was opened without an open panel.
*/
- (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError {
DocumentController *docController = [DocumentController sharedDocumentController];
return [self readFromURL:absoluteURL ofType:typeName encoding:[docController lastSelectedEncodingForURL:absoluteURL] ignoreRTF:[docController lastSelectedIgnoreRichForURL:absoluteURL] ignoreHTML:[docController lastSelectedIgnoreHTMLForURL:absoluteURL] error:outError];
}
- (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName encoding:(NSStringEncoding)encoding ignoreRTF:(BOOL)ignoreRTF ignoreHTML:(BOOL)ignoreHTML error:(NSError **)outError {
NSMutableDictionary *options = [NSMutableDictionary dictionaryWithCapacity:5];
NSDictionary *docAttrs;
id val, paperSizeVal, viewSizeVal;
NSTextStorage *text = [self textStorage];
/* generalize the passed-in type to a type we support. for instance, generalize "public.xml" to "public.txt" */
typeName = [[self class] readableTypeForType:typeName];
[fileTypeToSet release];
fileTypeToSet = nil;
[[self undoManager] disableUndoRegistration];
[options setObject:absoluteURL forKey:NSBaseURLDocumentOption];
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
if (encoding == NoStringEncoding &&
([workspace type:typeName conformsToType:(NSString *)kUTTypePlainText]))
encoding = [[EncodingManager sharedInstance] detectedEncodingForURL: absoluteURL];
if (encoding != NoStringEncoding) {
[options setObject:[NSNumber numberWithUnsignedInteger:encoding] forKey:NSCharacterEncodingDocumentOption];
}
[self setEncoding:encoding];
// Check type to see if we should load the document as plain. Note that this check isn't always conclusive, which is why we do another check below, after the document has been loaded (and correctly categorized).
if ((ignoreRTF && ([workspace type:typeName conformsToType:(NSString *)kUTTypeRTF] || [workspace type:typeName conformsToType:Word2003XMLType])) || (ignoreHTML && [workspace type:typeName conformsToType:(NSString *)kUTTypeHTML]) || [self isOpenedIgnoringRichText]) {
[options setObject:NSPlainTextDocumentType forKey:NSDocumentTypeDocumentOption]; // Force plain
typeName = (NSString *)kUTTypeText;
[self setOpenedIgnoringRichText:YES];
}
[[text mutableString] setString:@""];
// Remove the layout managers while loading the text; mutableCopy retains the array so the layout managers aren't released
NSMutableArray *layoutMgrs = [[text layoutManagers] mutableCopy];
NSEnumerator *layoutMgrEnum = [layoutMgrs objectEnumerator];
NSLayoutManager *layoutMgr = nil;
while ((layoutMgr = [layoutMgrEnum nextObject])) [text removeLayoutManager:layoutMgr];
// We can do this loop twice, if the document is loaded as rich text although the user requested plain
BOOL retry;
do {
BOOL success;
NSString *docType;
retry = NO;
[text beginEditing];
success = [text readFromURL:absoluteURL options:options documentAttributes:&docAttrs error:outError];
if (!success) {
[text endEditing];
layoutMgrEnum = [layoutMgrs objectEnumerator]; // rewind
while ((layoutMgr = [layoutMgrEnum nextObject])) [text addLayoutManager:layoutMgr]; // Add the layout managers back
[layoutMgrs release];
return NO; // return NO on error; outError has already been set
}
docType = [docAttrs objectForKey:NSDocumentTypeDocumentAttribute];
// First check to see if the document was rich and should have been loaded as plain
if (![[options objectForKey:NSDocumentTypeDocumentOption] isEqualToString:NSPlainTextDocumentType] && ((ignoreHTML && [docType isEqual:NSHTMLTextDocumentType]) || (ignoreRTF && ([docType isEqual:NSRTFTextDocumentType] || [docType isEqual:NSWordMLTextDocumentType])))) {
[text endEditing];
[[text mutableString] setString:@""];
[options setObject:NSPlainTextDocumentType forKey:NSDocumentTypeDocumentOption];
typeName = (NSString *)kUTTypeText;
[self setOpenedIgnoringRichText:YES];
retry = YES;
} else {
NSString *newFileType = [[self textDocumentTypeToTextEditDocumentTypeMappingTable] objectForKey:docType];
if (newFileType) {
typeName = newFileType;
} else {
typeName = (NSString *)kUTTypeRTF; // Hmm, a new type in the Cocoa text system. Treat it as rich. ??? Should set the converted flag too?
}
if (![[self class] isRichTextType:typeName]) [self applyDefaultTextAttributes:NO];
[text endEditing];
}
} while(retry);
[self setFileType:typeName];
// If we're reverting, NSDocument will set the file type behind out backs. This enables restoring that type.
fileTypeToSet = [typeName copy];
layoutMgrEnum = [layoutMgrs objectEnumerator]; // rewind
while ((layoutMgr = [layoutMgrEnum nextObject])) [text addLayoutManager:layoutMgr]; // Add the layout managers back
[layoutMgrs release];
val = [docAttrs objectForKey:NSCharacterEncodingDocumentAttribute];
[self setEncoding:(val ? [val unsignedIntegerValue] : NoStringEncoding)];
if ((val = [docAttrs objectForKey:NSConvertedDocumentAttribute])) {
[self setConverted:([val integerValue] > 0)]; // Indicates filtered
[self setLossy:([val integerValue] < 0)]; // Indicates lossily loaded
}
/* If the document has a stored value for view mode, use it. Otherwise wrap to window. */
if ((val = [docAttrs objectForKey:NSViewModeDocumentAttribute])) {
[self setHasMultiplePages:([val integerValue] == 1)];
if ((val = [docAttrs objectForKey:NSViewZoomDocumentAttribute])) {
[self setScaleFactor:([val doubleValue] / 100.0)];
}
} else [self setHasMultiplePages:NO];
[self willChangeValueForKey:@"printInfo"];
if ((val = [docAttrs objectForKey:NSLeftMarginDocumentAttribute])) [[self printInfo] setLeftMargin:[val doubleValue]];
if ((val = [docAttrs objectForKey:NSRightMarginDocumentAttribute])) [[self printInfo] setRightMargin:[val doubleValue]];
if ((val = [docAttrs objectForKey:NSBottomMarginDocumentAttribute])) [[self printInfo] setBottomMargin:[val doubleValue]];
if ((val = [docAttrs objectForKey:NSTopMarginDocumentAttribute])) [[self printInfo] setTopMargin:[val doubleValue]];
[self didChangeValueForKey:@"printInfo"];
/* Pre MacOSX versions of TextEdit wrote out the view (window) size in PaperSize.
If we encounter a non-MacOSX RTF file, and it's written by TextEdit, use PaperSize as ViewSize */
viewSizeVal = [docAttrs objectForKey:NSViewSizeDocumentAttribute];
paperSizeVal = [docAttrs objectForKey:NSPaperSizeDocumentAttribute];
if (paperSizeVal && NSEqualSizes([paperSizeVal sizeValue], NSZeroSize)) paperSizeVal = nil; // Protect against some old documents with 0 paper size
if (viewSizeVal) {
[self setViewSize:[viewSizeVal sizeValue]];
if (paperSizeVal) [self setPaperSize:[paperSizeVal sizeValue]];
} else { // No ViewSize...
if (paperSizeVal) { // See if PaperSize should be used as ViewSize; if so, we also have some tweaking to do on it
val = [docAttrs objectForKey:NSCocoaVersionDocumentAttribute];
if (val && ([val integerValue] < 100)) { // Indicates old RTF file; value described in AppKit/NSAttributedString.h
NSSize size = [paperSizeVal sizeValue];
if (size.width > 0 && size.height > 0 && ![self hasMultiplePages]) {
size.width = size.width - oldEditPaddingCompensation;
[self setViewSize:size];
}
} else {
[self setPaperSize:[paperSizeVal sizeValue]];
}
}
}
[self setHyphenationFactor:(val = [docAttrs objectForKey:NSHyphenationFactorDocumentAttribute]) ? [val floatValue] : 0];
[self setBackgroundColor:(val = [docAttrs objectForKey:NSBackgroundColorDocumentAttribute]) ? val : [NSColor whiteColor]];
// Set the document properties, generically, going through key value coding
NSDictionary *map = [self documentPropertyToAttributeNameMappings];
for (NSString *property in [self knownDocumentProperties]) [self setValue:[docAttrs objectForKey:[map objectForKey:property]] forKey:property]; // OK to set nil to clear
[self setReadOnly:((val = [docAttrs objectForKey:NSReadOnlyDocumentAttribute]) && ([val integerValue] > 0))];
[self setOriginalOrientationSections:[docAttrs objectForKey:NSTextLayoutSectionsAttribute]];
[[self undoManager] enableUndoRegistration];
return YES;
}
- (NSDictionary *)defaultTextAttributes:(BOOL)forRichText {
static NSParagraphStyle *defaultRichParaStyle = nil;
NSMutableDictionary *textAttributes = [[[NSMutableDictionary alloc] initWithCapacity:2] autorelease];
if (forRichText) {
[textAttributes setObject:[NSFont userFontOfSize:0.0] forKey:NSFontAttributeName];
if (defaultRichParaStyle == nil) { // We do this once...
NSInteger cnt;
NSString *measurementUnits = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleMeasurementUnits"];
CGFloat tabInterval = ([@"Centimeters" isEqual:measurementUnits]) ? (72.0 / 2.54) : (72.0 / 2.0); // Every cm or half inch
NSMutableParagraphStyle *paraStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
NSTextTabType type = ((NSWritingDirectionRightToLeft == [NSParagraphStyle defaultWritingDirectionForLanguage:nil]) ? NSRightTabStopType : NSLeftTabStopType);
[paraStyle setTabStops:[NSArray array]]; // This first clears all tab stops
for (cnt = 0; cnt < 12; cnt++) { // Add 12 tab stops, at desired intervals...
NSTextTab *tabStop = [[NSTextTab alloc] initWithType:type location:tabInterval * (cnt + 1)];
[paraStyle addTabStop:tabStop];
[tabStop release];
}
defaultRichParaStyle = [paraStyle copy];
}
[textAttributes setObject:defaultRichParaStyle forKey:NSParagraphStyleAttributeName];
} else {
NSFont *plainFont = [NSFont userFixedPitchFontOfSize:0.0];
NSInteger tabWidth = [[NSUserDefaults standardUserDefaults] integerForKey:TabWidth];
CGFloat charWidth = [@" " sizeWithAttributes:[NSDictionary dictionaryWithObject:plainFont forKey:NSFontAttributeName]].width;
if (charWidth == 0) charWidth = [[plainFont screenFontWithRenderingMode:NSFontDefaultRenderingMode] maximumAdvancement].width;
// Now use a default paragraph style, but with the tab width adjusted
NSMutableParagraphStyle *mStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
[mStyle setTabStops:[NSArray array]];
[mStyle setDefaultTabInterval:(charWidth * tabWidth)];
[textAttributes setObject:[[mStyle copy] autorelease] forKey:NSParagraphStyleAttributeName];
// Also set the font
[textAttributes setObject:plainFont forKey:NSFontAttributeName];
}
return textAttributes;
}
- (void)applyDefaultTextAttributes:(BOOL)forRichText {
NSDictionary *textAttributes = [self defaultTextAttributes:forRichText];
NSTextStorage *text = [self textStorage];
// We now preserve base writing direction even for plain text, using the 10.6-introduced attribute enumeration API
[text enumerateAttribute:NSParagraphStyleAttributeName inRange:NSMakeRange(0, [text length]) options:0 usingBlock:^(id paragraphStyle, NSRange paragraphStyleRange, BOOL *stop){
NSWritingDirection writingDirection = paragraphStyle ? [(NSParagraphStyle *)paragraphStyle baseWritingDirection] : NSWritingDirectionNatural;
// We also preserve NSWritingDirectionAttributeName (new in 10.6)
[text enumerateAttribute:NSWritingDirectionAttributeName inRange:paragraphStyleRange options:0 usingBlock:^(id value, NSRange attributeRange, BOOL *stop){
[value retain];
[text setAttributes:textAttributes range:attributeRange];
if (value) [text addAttribute:NSWritingDirectionAttributeName value:value range:attributeRange];
[value release];
}];
if (writingDirection != NSWritingDirectionNatural) [text setBaseWritingDirection:writingDirection range:paragraphStyleRange];
}];
}
/* This method will return a suggested encoding for the document. Since Mac OS X Leopard, unless the user has specified a favorite encoding for saving that applies to the document, we use UTF-8.
*/
- (NSStringEncoding)suggestedDocumentEncoding {
NSUInteger enc = NoStringEncoding;
NSNumber *val = [[NSUserDefaults standardUserDefaults] objectForKey:PlainTextEncodingForWrite];
if (val) {
NSStringEncoding chosenEncoding = [val unsignedIntegerValue];
if ((chosenEncoding != NoStringEncoding) && (chosenEncoding != NSUnicodeStringEncoding) && (chosenEncoding != NSUTF8StringEncoding)) {
if ([[[self textStorage] string] canBeConvertedToEncoding:chosenEncoding]) enc = chosenEncoding;
}
}
if (enc == NoStringEncoding) enc = NSUTF8StringEncoding; // Default to UTF-8
return enc;
}
/* Clear the delegates of the text views and window, then release all resources and go away...
*/
- (void)dealloc {
[textStorage release];
[backgroundColor release];
[author release];
[comment release];
[subject release];
[title release];
[keywords release];
[copyright release];
[fileTypeToSet release];
[originalOrientationSections release];
[saveOperationTypeLock release];
[super dealloc];
}
- (CGFloat)scaleFactor {
return scaleFactor;
}
- (void)setScaleFactor:(CGFloat)newScaleFactor {
scaleFactor = newScaleFactor;
}
- (NSSize)viewSize {
return viewSize;
}
- (void)setViewSize:(NSSize)size {
viewSize = size;
}
- (void)setReadOnly:(BOOL)flag {
isReadOnly = flag;
}
- (BOOL)isReadOnly {
return isReadOnly;
}
- (void)setBackgroundColor:(NSColor *)color {
id oldCol = backgroundColor;
backgroundColor = [color copy];
[oldCol release];
}
- (NSColor *)backgroundColor {
return backgroundColor;
}
- (NSTextStorage *)textStorage {
return textStorage;
}
- (NSSize)paperSize {
return [[self printInfo] paperSize];
}
- (void)setPaperSize:(NSSize)size {
NSPrintInfo *oldPrintInfo = [self printInfo];
if (!NSEqualSizes(size, [oldPrintInfo paperSize])) {
NSPrintInfo *newPrintInfo = [oldPrintInfo copy];
[newPrintInfo setPaperSize:size];
[self setPrintInfo:newPrintInfo];
[newPrintInfo release];
}
}
/* Layout orientation sections */
- (void)setOriginalOrientationSections:(NSArray *)array {
[originalOrientationSections release];
originalOrientationSections = [array copy];
}
- (NSArray *)originalOrientationSections {
return originalOrientationSections;
}
/* Hyphenation related methods.
*/
- (void)setHyphenationFactor:(float)factor {
hyphenationFactor = factor;
}
- (float)hyphenationFactor {
return hyphenationFactor;
}
/* Encoding...
*/
- (NSUInteger)encoding {
return documentEncoding;
}
- (void)setEncoding:(NSUInteger)encoding {
documentEncoding = encoding;
}
/* This is the encoding used for saving; valid only during a save operation
*/
- (NSUInteger)encodingForSaving {
return documentEncodingForSaving;
}
- (void)setEncodingForSaving:(NSUInteger)encoding {
documentEncodingForSaving = encoding;
}
- (BOOL)isConverted {
return convertedDocument;
}
- (void)setConverted:(BOOL)flag {
convertedDocument = flag;
}
- (BOOL)isLossy {
return lossyDocument;
}
- (void)setLossy:(BOOL)flag {
lossyDocument = flag;
}
- (BOOL)isOpenedIgnoringRichText {
return openedIgnoringRichText;
}
- (void)setOpenedIgnoringRichText:(BOOL)flag {
openedIgnoringRichText = flag;
}
/* A transient document is an untitled document that was opened automatically. If a real document is opened before the transient document is edited, the real document should replace the transient. If a transient document is edited, it ceases to be transient.
*/
- (BOOL)isTransient {
return transient;
}
- (void)setTransient:(BOOL)flag {
transient = flag;
}
/* We can't replace transient document that have sheets on them.
*/
- (BOOL)isTransientAndCanBeReplaced {
if (![self isTransient]) return NO;
for (NSWindowController *controller in [self windowControllers]) if ([[controller window] attachedSheet]) return NO;
return YES;
}
- (void)setFileType:(NSString *)type {
/* Due to sandboxing, we cannot (usefully) directly change the document's file URL except for changing it to nil. This means that when we convert a document from rich text to plain text, our only way of updating the file URL is to have NSDocument do it for us in response to a change in our file type. However, it is not as simple as setting our type to kUTTypeText, as would be accurate, because if we have a rtf document, public.rtf inherits from public.text and so NSDocument wouldn't change our extension (which is correct, BTW, since it's also perfectly valid to open a rtf and not interpret the rtf commands, in which case a save of a rtf document as kUTTypeText should not change the extension). Therefore we need to save using a subtype of kUTTypeText that isn't in the path from kUTTypeText to kUTTypeRTF. The obvious candidate is kUTTypePlainText. Therefore, we need to save using kUTTypePlainText when we convert a rtf to plain text and then map the file type from kTTypePlainText to kUTTypeText. The inverse of the mapping occurs here. */
if ([type isEqualToString:(NSString *)kUTTypePlainText]) type = (NSString *)kUTTypeText;
[super setFileType:type];
}
- (BOOL)isRichText {
return [[self class] isRichTextType:[self fileType]];
}
/* Document properties management */
/* Table mapping document property keys "company", etc, to text system document attribute keys (NSCompanyDocumentAttribute, etc)
*/
- (NSDictionary *)documentPropertyToAttributeNameMappings {
static NSDictionary *dict = nil;
if (!dict) dict = [[NSDictionary alloc] initWithObjectsAndKeys:
NSCompanyDocumentAttribute, @"company",
NSAuthorDocumentAttribute, @"author",
NSKeywordsDocumentAttribute, @"keywords",
NSCopyrightDocumentAttribute, @"copyright",
NSTitleDocumentAttribute, @"title",
NSSubjectDocumentAttribute, @"subject",
NSCommentDocumentAttribute, @"comment", nil];
return dict;
}
- (NSArray *)knownDocumentProperties {
return [[self documentPropertyToAttributeNameMappings] allKeys];
}
/* If there are document properties and they are not the same as the defaults established in preferences, return YES
*/
- (BOOL)hasDocumentProperties {
for (NSString *key in [self knownDocumentProperties]) {
id value = [self valueForKey:key];
if (value && ![value isEqual:[[NSUserDefaults standardUserDefaults] objectForKey:key]]) return YES;
}
return NO;
}
/* This actually clears all properties (rather than setting them to default values established in preferences)
*/
- (void)clearDocumentProperties {
for (NSString *key in [self knownDocumentProperties]) [self setValue:nil forKey:key];
}
/* This sets document properties to values established in defaults
*/
- (void)setDocumentPropertiesToDefaults {
for (NSString *key in [self knownDocumentProperties]) [self setValue:[[NSUserDefaults standardUserDefaults] objectForKey:key] forKey:key];
}
/* We implement a setValue:forDocumentProperty: to work around NSUndoManager bug where prepareWithInvocationTarget: fails to freeze-dry invocations with "known" methods such as setValue:forKey:.
*/
- (void)setValue:(id)value forDocumentProperty:(NSString *)property {
id oldValue = [self valueForKey:property];
[[[self undoManager] prepareWithInvocationTarget:self] setValue:oldValue forDocumentProperty:property];
[[self undoManager] setActionName:NSLocalizedString(property, "")]; // Potential strings for action names are listed below (for genstrings to pick up)
// Call the regular KVC mechanism to get the value to be properly set
[super setValue:value forKey:property];
}
- (void)setValue:(id)value forKey:(NSString *)key {
if ([[self knownDocumentProperties] containsObject:key]) {
[self setValue:value forDocumentProperty:key]; // We take a side-trip to this method to register for undo
} else {
[super setValue:value forKey:key]; // In case some other KVC call is sent to Document, we treat it normally
}
}
/* For genstrings:
NSLocalizedStringWithDefaultValue(@"author", @"", @"", @"Change Author", @"Undo menu change string, without the 'Undo'");
NSLocalizedStringWithDefaultValue(@"copyright", @"", @"", @"Change Copyright", @"Undo menu change string, without the 'Undo'");
NSLocalizedStringWithDefaultValue(@"subject", @"", @"", @"Change Subject", @"Undo menu change string, without the 'Undo'");
NSLocalizedStringWithDefaultValue(@"title", @"", @"", @"Change Title", @"Undo menu change string, without the 'Undo'");
NSLocalizedStringWithDefaultValue(@"company", @"", @"", @"Change Company", @"Undo menu change string, without the 'Undo'");
NSLocalizedStringWithDefaultValue(@"comment", @"", @"", @"Change Comment", @"Undo menu change string, without the 'Undo'");
NSLocalizedStringWithDefaultValue(@"keywords", @"", @"", @"Change Keywords", @"Undo menu change string, without the 'Undo'");
*/
- (NSPrintOperation *)printOperationWithSettings:(NSDictionary *)printSettings error:(NSError **)outError {
NSPrintInfo *tempPrintInfo = [[[self printInfo] copy] autorelease];
[[tempPrintInfo dictionary] addEntriesFromDictionary:printSettings];
BOOL multiPage = [self hasMultiplePages];
if ([[self windowControllers] count] == 0) [self makeWindowControllers];
NSView *documentView = [[[self windowControllers] objectAtIndex:0] documentView];
id printingView;
if (multiPage) { // If already in multiple-page ("wrap-to-page") mode, we simply use the display view for printing
printingView = documentView;
[[[[self windowControllers] objectAtIndex:0] firstTextView] textEditDoForegroundLayoutToCharacterIndex:NSIntegerMax]; // Make sure the whole document is laid out before printing
} else { // Otherwise we create a new text view (along with a text container and layout manager)
printingView = [[[PrintingTextView alloc] init] autorelease]; // PrintingTextView is a simple subclass of NSTextView. Creating the view this way creates rest of the text system, which it will release when dealloc'ed (since the print panel will be releasing this, we want to hand off the responsibility of release everything)
NSLayoutManager *layoutManager = [[[[printingView textContainer] layoutManager] retain] autorelease];
NSTextStorage *unnecessaryTextStorage = [layoutManager textStorage]; // We don't want the text storage, since we will use the one we have
[unnecessaryTextStorage removeLayoutManager:layoutManager];
[unnecessaryTextStorage release];
[textStorage addLayoutManager:layoutManager];
[textStorage retain]; // Since later release of the printingView will release the textStorage as well
[printingView setLayoutOrientation:[[[[self windowControllers] objectAtIndex:0] firstTextView] layoutOrientation]];
}
PrintPanelAccessoryController *accessoryController = [[[PrintPanelAccessoryController alloc] init] autorelease];
NSPrintOperation *op = [NSPrintOperation printOperationWithView:printingView printInfo:tempPrintInfo];
[op setShowsPrintPanel:YES];
[op setShowsProgressPanel:YES];
// Since this printing view may not be embedded in a window, we have to manually set the print job title.
[op setJobTitle:[documentView printJobTitle]];
// If we're in wrap-to-page mode, no need to let the user tweak wrap-to-page mode printing
[accessoryController setShowsWrappingToFit:!multiPage];
NSPrintPanel *printPanel = [op printPanel];
if (!multiPage) {
[printingView setOriginalSize:[[[[self windowControllers] objectAtIndex:0] firstTextView] frame].size];
[printingView setPrintPanelAccessoryController:accessoryController];
// We allow changing print parameters if not in "Wrap to Page" mode, where the page setup settings are used
[printPanel setOptions:[printPanel options] | NSPrintPanelShowsPaperSize | NSPrintPanelShowsOrientation];
}
[printPanel addAccessoryController:accessoryController];
return op;
}
- (NSPrintInfo *)printInfo {
NSPrintInfo *printInfo = [super printInfo];
if (!setUpPrintInfoDefaults) {
setUpPrintInfoDefaults = YES;
[printInfo setHorizontalPagination:NSFitPagination];
[printInfo setHorizontallyCentered:NO];
[printInfo setVerticallyCentered:NO];
[printInfo setLeftMargin:72.0];
[printInfo setRightMargin:72.0];
[printInfo setTopMargin:72.0];
[printInfo setBottomMargin:72.0];
}
return printInfo;
}
/* Toggles read-only state of the document
*/
- (IBAction)toggleReadOnly:(id)sender {
[[self undoManager] registerUndoWithTarget:self selector:@selector(toggleReadOnly:) object:nil];
[[self undoManager] setActionName:[self isReadOnly] ?
NSLocalizedString(@"Allow Editing", @"Menu item to make the current document editable (not read-only)") :
NSLocalizedString(@"Prevent Editing", @"Menu item to make the current document read-only")];
[self setReadOnly:![self isReadOnly]];
}
- (BOOL)toggleRichWillLoseInformation {
NSInteger length = [textStorage length];
NSRange range;
NSDictionary *attrs;
return ( ([self isRichText] // Only rich -> plain can lose information.
&& ((length > 0) // If the document contains characters and...
&& (attrs = [textStorage attributesAtIndex:0 effectiveRange:&range]) // ...they have attributes...
&& ((range.length < length) // ...which either are not the same for the whole document...
|| ![[self defaultTextAttributes:YES] isEqual:attrs]) // ...or differ from the default, then...
)) // ...we will lose styling information.
|| [self hasDocumentProperties]); // We will also lose information if the document has properties.
}
- (BOOL)hasMultiplePages {
return hasMultiplePages;
}
- (void)setHasMultiplePages:(BOOL)flag {
hasMultiplePages = flag;
}
- (IBAction)togglePageBreaks:(id)sender {
[self setHasMultiplePages:![self hasMultiplePages]];
}
- (void)toggleHyphenation:(id)sender {
float currentHyphenation = [self hyphenationFactor];
[[[self undoManager] prepareWithInvocationTarget:self] setHyphenationFactor:currentHyphenation];
[self setHyphenationFactor:(currentHyphenation > 0.0) ? 0.0 : 0.9]; /* Toggle between 0.0 and 0.9 */
}
/* Action method for the "Append '.txt' extension" button
*/
- (void)appendPlainTextExtensionChanged:(id)sender {
NSSavePanel *panel = (NSSavePanel *)[sender window];
[panel setAllowsOtherFileTypes:[sender state]];
[panel setAllowedFileTypes:[sender state] ? [NSArray arrayWithObject:(NSString *)kUTTypePlainText] : nil];
}
- (void)encodingPopupChanged:(NSPopUpButton *)popup {
[self setEncodingForSaving:[[[popup selectedItem] representedObject] unsignedIntegerValue]];
}
/* Menu validation: Arbitrary numbers to determine the state of the menu items whose titles change. Speeds up the validation... Not zero. */
#define TagForFirst 42
#define TagForSecond 43
void validateToggleItem(NSMenuItem *aCell, BOOL useFirst, NSString *first, NSString *second) {
if (useFirst) {
if ([aCell tag] != TagForFirst) {
[aCell setTitleWithMnemonic:first];
[aCell setTag:TagForFirst];
}
} else {
if ([aCell tag] != TagForSecond) {
[aCell setTitleWithMnemonic:second];
[aCell setTag:TagForSecond];
}
}
}
/* Menu validation
*/
- (BOOL)validateMenuItem:(NSMenuItem *)aCell {
SEL action = [aCell action];
if (action == @selector(toggleReadOnly:)) {
validateToggleItem(aCell, [self isReadOnly], NSLocalizedString(@"Allow Editing", @"Menu item to make the current document editable (not read-only)"), NSLocalizedString(@"Prevent Editing", @"Menu item to make the current document read-only"));
return YES;
} else if (action == @selector(togglePageBreaks:)) {
validateToggleItem(aCell, [self hasMultiplePages], NSLocalizedString(@"&Wrap to Window", @"Menu item to cause text to be laid out to size of the window"), NSLocalizedString(@"&Wrap to Page", @"Menu item to cause text to be laid out to the size of the currently selected page type"));
return YES;
} else if (action == @selector(toggleHyphenation:)) {
validateToggleItem(aCell, ([self hyphenationFactor] > 0.0), NSLocalizedString(@"Do not Allow Hyphenation", @"Menu item to disallow hyphenation in the document"), NSLocalizedString(@"Allow Hyphenation", @"Menu item to allow hyphenation in the document"));
if ([self isReadOnly]) return NO;
return YES;
}
return [super validateMenuItem:aCell];
}
// For scripting. We already have a -textStorage method implemented above.
- (void)setTextStorage:(id)ts {
// Warning, undo support can eat a lot of memory if a long text is changed frequently
NSAttributedString *textStorageCopy = [[self textStorage] copy];
[[self undoManager] registerUndoWithTarget:self selector:@selector(setTextStorage:) object:textStorageCopy];
[textStorageCopy release];
// ts can actually be a string or an attributed string.
if ([ts isKindOfClass:[NSAttributedString class]]) {
[[self textStorage] replaceCharactersInRange:NSMakeRange(0, [[self textStorage] length]) withAttributedString:ts];
} else {
[[self textStorage] replaceCharactersInRange:NSMakeRange(0, [[self textStorage] length]) withString:ts];
}
}
- (BOOL)revertToContentsOfURL:(NSURL *)url ofType:(NSString *)type error:(NSError **)outError {
BOOL success = [super revertToContentsOfURL:url ofType:type error:outError];
if (success) {
if (fileTypeToSet) { // If we're reverting, NSDocument will set the file type behind out backs. This enables restoring that type.
[self setFileType:fileTypeToSet];
[fileTypeToSet release];
fileTypeToSet = nil;
}
[self setHasMultiplePages:hasMultiplePages];
[[self windowControllers] makeObjectsPerformSelector:@selector(setupTextViewForDocument)];
}
return success;
}
/* Target/action method for saving as (actually "saving to") PDF. Note that this approach of omitting the path will not work on Leopard; see TextEdit's README.rtf
*/
- (IBAction)saveDocumentAsPDFTo:(id)sender {
[self printDocumentWithSettings:[NSDictionary dictionaryWithObjectsAndKeys:NSPrintSaveJob, NSPrintJobDisposition, nil] showPrintPanel:NO delegate:nil didPrintSelector:NULL contextInfo:NULL];
}
@end
/* Returns the default padding on the left/right edges of text views
*/
CGFloat defaultTextPadding(void) {
static CGFloat padding = -1;
if (padding < 0.0) {
NSTextContainer *container = [[NSTextContainer alloc] init];
padding = [container lineFragmentPadding];
[container release];
}
return padding;
}
@implementation Document (TextEditNSDocumentOverrides)
+ (BOOL)autosavesInPlace {
return YES;
}
+ (BOOL)canConcurrentlyReadDocumentsOfType:(NSString *)typeName {
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
return !([workspace type:typeName conformsToType:(NSString *)kUTTypeHTML] || [workspace type:typeName conformsToType:(NSString *)kUTTypeWebArchive]);
}
- (void)makeWindowControllers {
NSArray *myControllers = [self windowControllers];
// If this document displaced a transient document, it will already have been assigned a window controller. If that is not the case, create one.
if ([myControllers count] == 0)
{
//ide kell kod
[self addWindowController:[[[DocumentWindowController allocWithZone:[self zone]] init] autorelease]];
}
}
/* One of the determinants of whether a file is locked is whether its type is one of our writable types. However, the writable types are a function of whether the document contains attachments. But whether we are locked cannot be a function of whether the document contains attachments, because we won't be asked to redetermine autosaving safety after an undo operation resulting from a cancel, so the document would continue to appear locked if an image were dragged in and then cancel was pressed. Therefore, we must use an "ignoreTemporary" boolean to treat RTF as temporarily writable despite the attachments. That's fine since -checkAutosavingSafetyAfterChangeAndReturnError: will perform this check again with ignoreTemporary set to NO, and that method will be called when the operation is done and undone, so no inconsistency results.
*/
- (NSArray *)writableTypesForSaveOperation:(NSSaveOperationType)saveOperation ignoreTemporaryState:(BOOL)ignoreTemporary {
NSMutableArray *outArray = [[[[self class] writableTypes] mutableCopy] autorelease];
if (saveOperation == NSSaveAsOperation) {
// Rich-text documents cannot be saved as plain text.
if ([self isRichText]) {
[outArray removeObject:(NSString *)kUTTypeText];
}
// Documents that contain attachments can only be saved in formats that support embedded graphics.
if (!ignoreTemporary && [textStorage containsAttachments]) {
[outArray setArray:[NSArray arrayWithObjects:(NSString *)kUTTypeRTFD, (NSString *)kUTTypeWebArchive, nil]];
}
}
return outArray;
}
- (NSArray *)writableTypesForSaveOperation:(NSSaveOperationType)saveOperation {
return [self writableTypesForSaveOperation:saveOperation ignoreTemporaryState:NO];
}
- (NSString *)fileNameExtensionForType:(NSString *)inTypeName saveOperation:(NSSaveOperationType)inSaveOperation {
/* We use kUTTypeText as our plain text type. However, kUTTypeText is really a class of types and therefore contains no preferred extension. Therefore we must specify a preferred extension, that of kUTTypePlainText. */
if ([inTypeName isEqualToString:(NSString *)kUTTypeText]) return @"txt";
return [super fileNameExtensionForType:inTypeName saveOperation:inSaveOperation];
}
/* When we save, we send a notification so that views that are currently coalescing undo actions can break that. This is done for two reasons, one technical and the other HI oriented.
Firstly, since the dirty state tracking is based on undo, for a coalesced set of changes that span over a save operation, the changes that occur between the save and the next time the undo coalescing stops will not mark the document as dirty. Secondly, allowing the user to undo back to the precise point of a save is good UI.
In addition we overwrite this method as a way to tell that the document has been saved successfully. If so, we set the save time parameters in the document.
*/
- (void)saveToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName forSaveOperation:(NSSaveOperationType)saveOperation completionHandler:(void (^)(NSError *error))handler {
// Note that we do the breakUndoCoalescing call even during autosave, which means the user's undo of long typing will take them back to the last spot an autosave occured. This might seem confusing, and a more elaborate solution may be possible (cause an autosave without having to breakUndoCoalescing), but since this change is coming late in Leopard, we decided to go with the lower risk fix.
[[self windowControllers] makeObjectsPerformSelector:@selector(breakUndoCoalescing)];
[saveOperationTypeLock lock];
currentSaveOperation = saveOperation;
handler = [Block_copy(handler) autorelease];
[super saveToURL:absoluteURL ofType:typeName forSaveOperation:saveOperation completionHandler:^(NSError *error) {
[self setEncodingForSaving:NoStringEncoding]; // This is set during prepareSavePanel:, but should be cleared for future save operation without save panel
[saveOperationTypeLock unlock];
handler(error);
}];
}
/* Indicate the types we know we can save safely asynchronously.
*/
- (BOOL)canAsynchronouslyWriteToURL:(NSURL *)url ofType:(NSString *)typeName forSaveOperation:(NSSaveOperationType)saveOperation {
return [[self class] canConcurrentlyReadDocumentsOfType:typeName];
}
- (NSError *)errorInTextEditDomainWithCode:(NSInteger)errorCode {
switch (errorCode) {
case TextEditSaveErrorWritableTypeRequired: {
NSString *description, *recoverySuggestion;
/* the document can't be saved in its original format, either because TextEdit cannot write to the format, or TextEdit cannot write documents containing
attachments to the format. */
if ([textStorage containsAttachments]) {
description = NSLocalizedString(@"Convert this document to RTFD format?",
@"Title of alert panel prompting the user to convert to RTFD.");
recoverySuggestion = NSLocalizedString(
@"Documents with graphics and attachments will be saved using RTFD (RTF with graphics) format. RTFD documents are not compatible with some applications. Convert anyway?",
@"Contents of alert panel prompting the user to convert to RTFD.");
} else {
description = NSLocalizedString(@"Convert this document to RTF format?",
@"Title of alert panel prompting the user to convert to RTF.");
recoverySuggestion = NSLocalizedString(@"This document must be converted to RTF before it can be modified.",
@"Contents of alert panel prompting the user to convert to RTF.");
}
return [NSError errorWithDomain:TextEditErrorDomain code:TextEditSaveErrorWritableTypeRequired userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
description, NSLocalizedDescriptionKey,
recoverySuggestion, NSLocalizedRecoverySuggestionErrorKey,
[NSArray arrayWithObjects:
NSLocalizedString(@"Convert", @"Button choice that allows the user to convert the document."),
NSLocalizedString(@"Cancel", @"Button choice that allows the user to cancel."),
NSLocalizedString(@"Duplicate", @"Button choice that allows the user to duplicate the document.")
, nil], NSLocalizedRecoveryOptionsErrorKey,
self, NSRecoveryAttempterErrorKey,
nil]];
}
case TextEditSaveErrorConvertedDocument: {
NSString *newFormatName = [textStorage containsAttachments] ? NSLocalizedString(@"rich text with graphics (RTFD)", @"Rich text with graphics file format name, displayed in alert")
: NSLocalizedString(@"rich text", @"Rich text file format name, displayed in alert");
return [NSError errorWithDomain:TextEditErrorDomain code:TextEditSaveErrorConvertedDocument userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
NSLocalizedString(
@"Are you sure you want to edit this document?",
@"Title of alert panel asking the user whether he wants to edit a converted document."), NSLocalizedDescriptionKey,
[NSString stringWithFormat:NSLocalizedString(@"This document was converted from a format that TextEdit cannot save. It will be saved in %@ format.",
@"Contents of alert panel informing user that the document is converted and cannot be written in its original format."), newFormatName], NSLocalizedRecoverySuggestionErrorKey,
[NSArray arrayWithObjects:
NSLocalizedString(@"Edit", @"Button choice that allows the user to save the document."),
NSLocalizedString(@"Cancel", @"Button choice that allows the user to cancel."),
NSLocalizedString(@"Duplicate", @"Button choice that allows the user to duplicate the document.")
, nil], NSLocalizedRecoveryOptionsErrorKey,
self, NSRecoveryAttempterErrorKey,
nil]];
}
case TextEditSaveErrorLossyDocument: {
return [NSError errorWithDomain:TextEditErrorDomain code:TextEditSaveErrorLossyDocument userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
NSLocalizedString(@"Are you sure you want to modify the document in place?", @"Title of alert panel which brings up a warning about saving over the same document"), NSLocalizedDescriptionKey,
NSLocalizedString(@"Modifying the document in place might cause you to lose some of the original formatting. Would you like to duplicate the document first?", @"Contents of alert panel informing user that they need to supply a new file name because the save might be lossy"), NSLocalizedRecoverySuggestionErrorKey,
[NSArray arrayWithObjects:
NSLocalizedString(@"Duplicate", @"Button choice that allows the user to duplicate the document."),
NSLocalizedString(@"Cancel", @"Button choice that allows the user to cancel."),
NSLocalizedString(@"Overwrite", @"Button choice allowing user to overwrite the document."), nil], NSLocalizedRecoveryOptionsErrorKey,
self, NSRecoveryAttempterErrorKey,
nil]];
}
case TextEditSaveErrorEncodingInapplicable: {
NSUInteger enc = [self encodingForSaving];
if (enc == NoStringEncoding) enc = [self encoding];
return [NSError errorWithDomain:TextEditErrorDomain code:TextEditSaveErrorEncodingInapplicable userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:NSLocalizedString(@"This document can no longer be saved using its original %@ encoding.", @"Title of alert panel informing user that the file's string encoding needs to be changed."), [NSString localizedNameOfStringEncoding:enc]], NSLocalizedDescriptionKey,
NSLocalizedString(@"Please choose another encoding (such as UTF-8).", @"Subtitle of alert panel informing user that the file's string encoding needs to be changed"), NSLocalizedRecoverySuggestionErrorKey,
NSLocalizedString(@"The specified text encoding isn\\U2019t applicable.",
@"Failure reason stating that the text encoding is not applicable."), NSLocalizedFailureReasonErrorKey,
[NSArray arrayWithObjects:
NSLocalizedString(@"OK", @"OK"),
NSLocalizedString(@"Cancel", @"Button choice that allows the user to cancel."), nil], NSLocalizedRecoveryOptionsErrorKey,
self, NSRecoveryAttempterErrorKey,
nil]];
}
}
return nil;
}
- (BOOL)checkAutosavingSafetyAfterChangeAndReturnError:(NSError **)outError {
BOOL safe = YES;
// it the document isn't saved, don't complain about limitations of its supposed backing store.
if ([self fileURL]) {
if (![[self writableTypesForSaveOperation:NSSaveAsOperation] containsObject:[self fileType]]) {
if (outError) *outError = [self errorInTextEditDomainWithCode:TextEditSaveErrorWritableTypeRequired];
safe = NO;
} else if (![self isRichText]) {
NSUInteger encoding = [self encoding];
if (encoding != NoStringEncoding && ![[textStorage string] canBeConvertedToEncoding:encoding]) {
if (outError) *outError = [self errorInTextEditDomainWithCode:TextEditSaveErrorEncodingInapplicable];
safe = NO;
}
}
}
return safe;
}
- (void)updateChangeCount:(NSDocumentChangeType)change {
NSError *error;
// When a document is changed, it ceases to be transient.
[self setTransient:NO];
[super updateChangeCount:change];
if (change == NSChangeDone || change == NSChangeRedone) {
// If we don't have a file URL, we can change our backing store type without consulting the user.
// NSDocument will update the extension of our autosaving location.
// If we don't do this, we won't be able to store images in autosaved untitled documents.