-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathDocumentWindowController.m
1219 lines (1008 loc) · 56.5 KB
/
DocumentWindowController.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: DocumentWindowController.m
Abstract: Document's main window controller object for TextEdit.
Version: 1.9
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) 2013 Apple Inc. All Rights Reserved.
*/
#import "DocumentWindowController.h"
#import "Document.h"
#import "MultiplePageView.h"
#import "TextEditDefaultsKeys.h"
#import "TextEditMisc.h"
#import "TextEditErrors.h"
#import "JJTypesetter.h"
#include "opencc.h"
@interface DocumentWindowController(Private)
- (void)setDocument:(Document *)doc; // Overridden with more specific type. Expects Document instance.
- (void)setupInitialTextViewSharedState;
- (void)setupTextViewForDocument;
- (void)setupWindowForDocument;
- (void)setupPagesViewForLayoutOrientation:(NSTextLayoutOrientation)orientation;
- (void)updateForRichTextAndRulerState:(BOOL)rich;
- (void)autosaveIfNeededThenToggleRich;
- (void)toggleRichWithNewFileType:(NSString *)fileType;
- (void)showRulerDelayed:(BOOL)flag;
- (void)addPage;
- (void)removePage;
- (NSTextView *)firstTextView;
- (void)printInfoUpdated;
- (void)resizeWindowForViewSize:(NSSize)size;
- (void)setHasMultiplePages:(BOOL)pages force:(BOOL)force;
@end
@implementation DocumentWindowController
- (id)init {
if (self = [super initWithWindowNibName:@"DocumentWindow"]) {
layoutMgr = [[NSLayoutManager allocWithZone:[self zone]] init];
[layoutMgr setDelegate:self];
[layoutMgr setAllowsNonContiguousLayout:YES];
NSBundle* bundle = [NSBundle mainBundle];
NSString* path = [[bundle resourcePath] stringByAppendingPathComponent:[NSString stringWithUTF8String:OPENCC_DEFAULT_CONFIG_TRAD_TO_SIMP]];
conversionHandle = opencc_open([path fileSystemRepresentation]);
}
return self;
}
- (void)dealloc {
if ([self document]) [self setDocument:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[self firstTextView] removeObserver:self forKeyPath:@"backgroundColor"];
[scrollView removeObserver:self forKeyPath:@"scaleFactor"];
[[scrollView verticalScroller] removeObserver:self forKeyPath:@"scrollerStyle"];
[layoutMgr release];
[self showRulerDelayed:NO];
if (conversionHandle != (opencc_t)-1)
opencc_close(conversionHandle);
[super dealloc]; // NSWindowController deallocates all the nib objects
}
/* This method can be called in three different situations (number three is a special TextEdit case):
1) When the window controller is created and set up with a new or opened document. (!oldDoc && doc)
2) When the document is closed, and the controller is about to be destroyed (oldDoc && !doc)
3) When the window controller is assigned to another document (a document has been opened and it takes the place of an automatically-created window). In that case this method is called twice. First as #2 above, second as #1.
The window can be visible or hidden at the time of the message.
*/
- (void)setDocument:(Document *)doc {
Document *oldDoc = [[self document] retain];
if (oldDoc) {
[layoutMgr unbind:@"hyphenationFactor"];
[[self firstTextView] unbind:@"editable"];
}
[super setDocument:doc];
if (doc) {
[layoutMgr bind:@"hyphenationFactor" toObject:self withKeyPath:@"document.hyphenationFactor" options:nil];
[[self firstTextView] bind:@"editable" toObject:self withKeyPath:@"document.readOnly" options:[NSDictionary dictionaryWithObject:NSNegateBooleanTransformerName forKey:NSValueTransformerNameBindingOption]];
}
if (oldDoc != doc) {
if (oldDoc) {
/* Remove layout manager from the old Document's text storage. No need to retain as we already own the object. */
[[oldDoc textStorage] removeLayoutManager:layoutMgr];
[oldDoc removeObserver:self forKeyPath:@"printInfo"];
[oldDoc removeObserver:self forKeyPath:@"richText"];
[oldDoc removeObserver:self forKeyPath:@"viewSize"];
[oldDoc removeObserver:self forKeyPath:@"hasMultiplePages"];
}
if (doc) {
JJTypesetter *typesetter = [[JJTypesetter alloc] init];
[typesetter setEnabled: [doc isRichText] ? NO : YES];
[layoutMgr setTypesetter:typesetter];
[[doc textStorage] addLayoutManager:layoutMgr];
[typesetter release];
if ([self isWindowLoaded]) {
[self setHasMultiplePages:[doc hasMultiplePages] force:NO];
[self setupInitialTextViewSharedState];
[self setupWindowForDocument];
if ([doc hasMultiplePages]) [scrollView setScaleFactor:[[self document] scaleFactor] adjustPopup:YES];
[[doc undoManager] removeAllActions];
}
[doc addObserver:self forKeyPath:@"printInfo" options:0 context:NULL];
[doc addObserver:self forKeyPath:@"richText" options:0 context:NULL];
[doc addObserver:self forKeyPath:@"viewSize" options:0 context:NULL];
[doc addObserver:self forKeyPath:@"hasMultiplePages" options:0 context:NULL];
}
}
[oldDoc release];
}
- (void)breakUndoCoalescing {
[[self firstTextView] breakUndoCoalescing];
}
- (NSLayoutManager *)layoutManager {
return layoutMgr;
}
- (NSTextView *)firstTextView {
return [[self layoutManager] firstTextView];
}
- (void)setupInitialTextViewSharedState {
NSTextView *textView = [self firstTextView];
[textView setUsesFontPanel:YES];
[textView setUsesFindBar:YES];
[textView setIncrementalSearchingEnabled:YES];
[textView setDelegate:self];
[textView setAllowsUndo:YES];
[textView setAllowsDocumentBackgroundColorChange:YES];
[textView setIdentifier:@"First Text View"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// Some settings are not enabled for plain text docs if the default "SubstitutionsEnabledInRichTextOnly" is set to YES.
// There is no UI at this stage for this preference.
BOOL substitutionsOK = [[self document] isRichText] || ![defaults boolForKey:SubstitutionsEnabledInRichTextOnly];
[textView setContinuousSpellCheckingEnabled:[defaults boolForKey:CheckSpellingAsYouType]];
[textView setGrammarCheckingEnabled:[defaults boolForKey:CheckGrammarWithSpelling]];
[textView setAutomaticSpellingCorrectionEnabled:substitutionsOK && [defaults boolForKey:CorrectSpellingAutomatically]];
[textView setSmartInsertDeleteEnabled:[defaults boolForKey:SmartCopyPaste]];
[textView setAutomaticQuoteSubstitutionEnabled:substitutionsOK && [defaults boolForKey:SmartQuotes]];
[textView setAutomaticDashSubstitutionEnabled:substitutionsOK && [defaults boolForKey:SmartDashes]];
[textView setAutomaticLinkDetectionEnabled:[defaults boolForKey:SmartLinks]];
[textView setAutomaticDataDetectionEnabled:[defaults boolForKey:DataDetectors]];
[textView setAutomaticTextReplacementEnabled:substitutionsOK && [defaults boolForKey:TextReplacement]];
[textView setSelectedRange:NSMakeRange(0, 0)];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == [self firstTextView]) {
if ([keyPath isEqualToString:@"backgroundColor"]) {
[[self document] setBackgroundColor:[[self firstTextView] backgroundColor]];
}
} else if (object == scrollView) {
if ([keyPath isEqualToString:@"scaleFactor"]) {
[[self document] setScaleFactor:[scrollView scaleFactor]];
}
} else if (object == [scrollView verticalScroller]) {
if ([keyPath isEqualToString:@"scrollerStyle"]) {
[self invalidateRestorableState];
NSSize size = [[self document] viewSize];
if (!NSEqualSizes(size, NSZeroSize)) {
[self resizeWindowForViewSize:size];
}
}
} else if (object == [self document]) {
if ([keyPath isEqualToString:@"printInfo"]) {
[self printInfoUpdated];
} else if ([keyPath isEqualToString:@"viewSize"]) {
if (!isSettingSize) {
NSSize size = [[self document] viewSize];
if (!NSEqualSizes(size, NSZeroSize)) {
[self resizeWindowForViewSize:size];
}
}
} else if ([keyPath isEqualToString:@"hasMultiplePages"]) {
[self setHasMultiplePages:[[self document] hasMultiplePages] force:NO];
}
}
}
- (void)setupTextViewForDocument {
Document *doc = [self document];
NSArray *sections = [doc originalOrientationSections];
NSTextLayoutOrientation orientation = NSTextLayoutOrientationHorizontal;
BOOL rich = [doc isRichText];
if (doc && (!rich || [[[self firstTextView] textStorage] length] == 0)) [[self firstTextView] setTypingAttributes:[doc defaultTextAttributes:rich]];
[self updateForRichTextAndRulerState:rich];
[[self firstTextView] setBackgroundColor:[doc backgroundColor]];
[[[self firstTextView] layoutManager] setUsesScreenFonts:[doc usesScreenFonts]];
// process the initial container
if ([sections count] > 0) {
for (NSDictionary *dict in sections) {
id rangeValue = [dict objectForKey:NSTextLayoutSectionRange];
if (!rangeValue || NSLocationInRange(0, [rangeValue rangeValue])) {
orientation = NSTextLayoutOrientationVertical;
[[self firstTextView] setLayoutOrientation:orientation];
break;
}
}
}
if (hasMultiplePages && (orientation != NSTextLayoutOrientationHorizontal)) [self setupPagesViewForLayoutOrientation:orientation];
}
- (void)printInfoUpdated {
if (hasMultiplePages) {
NSUInteger cnt;
MultiplePageView *pagesView = [scrollView documentView];
NSArray *textContainers = [[self layoutManager] textContainers];
[pagesView setPrintInfo:[[self document] printInfo]];
for (cnt = 0; cnt < [self numberOfPages]; cnt++) { // Call -numberOfPages repeatedly since it may change
NSRect textFrame = [pagesView documentRectForPageNumber:cnt];
NSTextContainer *textContainer = [textContainers objectAtIndex:cnt];
[textContainer setContainerSize:textFrame.size];
[[textContainer textView] setFrame:textFrame];
}
}
}
/* Method to lazily display ruler. Call with YES to display, NO to cancel display; this method doesn't remove the ruler.
*/
- (void)showRulerDelayed:(BOOL)flag {
if (!flag && rulerIsBeingDisplayed) {
[[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(showRuler:) object:self];
} else if (flag && !rulerIsBeingDisplayed) {
[self performSelector:@selector(showRuler:) withObject:self afterDelay:0.0];
}
rulerIsBeingDisplayed = flag;
}
- (void)showRuler:(id)obj {
if (rulerIsBeingDisplayed && !obj) [self showRulerDelayed:NO]; // Cancel outstanding request, if not coming from the delayed request
if ([[NSUserDefaults standardUserDefaults] boolForKey:ShowRuler]) [[self firstTextView] setRulerVisible:YES];
}
/* Used when converting to plain text
*/
- (void)removeAttachments {
NSTextStorage *attrString = [[self document] textStorage];
NSTextView *view = [self firstTextView];
NSUInteger loc = 0;
NSUInteger end = [attrString length];
[attrString beginEditing];
while (loc < end) { /* Run through the string in terms of attachment runs */
NSRange attachmentRange; /* Attachment attribute run */
NSTextAttachment *attachment = [attrString attribute:NSAttachmentAttributeName atIndex:loc longestEffectiveRange:&attachmentRange inRange:NSMakeRange(loc, end-loc)];
if (attachment) { /* If there is an attachment and it is on an attachment character, remove the character */
unichar ch = [[attrString string] characterAtIndex:loc];
if (ch == NSAttachmentCharacter) {
if ([view shouldChangeTextInRange:NSMakeRange(loc, 1) replacementString:@""]) {
[attrString replaceCharactersInRange:NSMakeRange(loc, 1) withString:@""];
[view didChangeText];
}
end = [attrString length]; /* New length */
}
else loc++; /* Just skip over the current character... */
}
else loc = NSMaxRange(attachmentRange);
}
[attrString endEditing];
}
/* This method implements panel-based "attach" functionality. Note that as-is, it's set to accept all files; however, by setting allowed types on the open panel it can be restricted to images, etc.
*/
- (void)presenterDidPresent:(BOOL)inDidSucceed soContinue:(void (^)(BOOL didSucceed))inContinuerCopy {
inContinuerCopy(inDidSucceed);
Block_release(inContinuerCopy);
}
- (void)chooseAndAttachFiles:(id)sender {
[[self document] performActivityWithSynchronousWaiting:YES usingBlock:^(void (^activityCompletionHandler)(void)) {
NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel setCanChooseDirectories:YES];
[panel setAllowsMultipleSelection:YES];
// Use the 10.6-introduced sheet API with block handler
[panel beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result) {
if (result == NSFileHandlingPanelOKButton) { // Only if not cancelled
NSArray *urls = [panel URLs];
NSTextView *textView = [self firstTextView];
NSInteger numberOfErrors = 0;
NSError *error = nil;
NSMutableAttributedString *attachments = [[NSMutableAttributedString alloc] init];
// Process all the attachments, creating an attributed string
for (NSURL *url in urls) {
NSFileWrapper *wrapper = [[NSFileWrapper alloc] initWithURL:url options:NSFileWrapperReadingImmediate error:&error];
if (wrapper) {
NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithFileWrapper:wrapper];
[attachments appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];
[wrapper release];
[attachment release];
} else {
numberOfErrors++;
}
}
// We could actually take an approach where on partial success we allow the user to cancel the operation, but since it's easy enough to undo, this seems reasonable enough
if ([attachments length] > 0) {
NSRange selectionRange = [textView selectedRange];
if ([textView shouldChangeTextInRange:selectionRange replacementString:[attachments string]]) { // Shouldn't fail, since we are controlling the text view; but if it does, we simply don't allow the change
[[textView textStorage] replaceCharactersInRange:selectionRange withAttributedString:attachments];
[textView didChangeText];
}
}
[attachments release];
[panel orderOut:nil]; // Strictly speaking not necessary, but if we put up an error sheet, a good idea for the panel to be dismissed first
// Deal with errors opening some or all of the attachments
if (numberOfErrors > 0) {
if (numberOfErrors > 1) { // More than one failure, put up a summary error (which doesn't do a good job of communicating the actual errors, but multiple attachments is a relatively rare case). For one error, we present the actual NSError we got back.
// The error message will be different depending on whether all or some of the files were successfully attached.
NSString *description = (numberOfErrors == [urls count]) ? NSLocalizedString(@"None of the items could be attached.", @"Title of alert indicating error during 'Attach Files...' when user tries to attach (insert) multiple files and none can be attached.") : NSLocalizedString(@"Some of the items could not be attached.", @"Title of alert indicating error during 'Attach Files...' when user tries to attach (insert) multiple files and some fail.");
error = [NSError errorWithDomain:TextEditErrorDomain code:TextEditAttachFilesFailure userInfo:[NSDictionary dictionaryWithObjectsAndKeys:description, NSLocalizedDescriptionKey, NSLocalizedString(@"The files may be unreadable, or the volume they are on may be inaccessible. Please check in Finder.", @"Recommendation when 'Attach Files...' command fails"), NSLocalizedRecoverySuggestionErrorKey, nil]];
}
[[self window] presentError:error modalForWindow:[self window] delegate:self didPresentSelector:@selector(presenterDidPresent:soContinue:) contextInfo:Block_copy(^(BOOL didSucceed) {
activityCompletionHandler();
})];
} else {
activityCompletionHandler();
}
} else {
activityCompletionHandler();
}
}];
}];
}
- (IBAction)convertToSimplifiedChinese:(id)sender {
if (conversionHandle == (opencc_t)-1)
return;
NSTextStorage *textStorage = [[self document] textStorage];
NSRange selectedRange = [[self firstTextView] selectedRange];
NSString* contents = [[textStorage attributedSubstringFromRange:selectedRange] string];
const char* inbuf = [contents UTF8String];
char* buf = opencc_convert_utf8(conversionHandle, inbuf, -1);
NSString* convertedString = [NSString stringWithUTF8String:buf];
opencc_convert_utf8_free(buf);
if ([[self firstTextView] shouldChangeTextInRange:selectedRange
replacementString:convertedString]) {
[textStorage beginEditing];
[textStorage replaceCharactersInRange:selectedRange withString:convertedString];
[[self firstTextView] didChangeText];
[textStorage endEditing];
}
}
#define outputLine(output, line) [output appendFormat: @"%@\n", line]
#define outputParagraph(output, paragraph) do { \
[output appendFormat: @"%@\n\n", paragraph]; \
[paragraph release]; \
paragraph = nil; \
} while (0)
/* Action method for unwrap lines. */
- (IBAction)unwrapSelectedLines:(id)sender {
NSTextStorage *textStorage = [[self document] textStorage];
NSRange selectedRange = [[self firstTextView] selectedRange];
NSString* contents = [[textStorage attributedSubstringFromRange:selectedRange] string];
NSUInteger end = 0;
BOOL inParagraph = NO;
NSCharacterSet *newlineSet = [NSCharacterSet newlineCharacterSet];
NSMutableString *output = [[NSMutableString alloc] init];
NSMutableString *paragraph = nil;
for (NSRange range = NSMakeRange(0, 0);
end < [contents length];
range.location = end)
{
NSUInteger start = 0;
[contents getLineStart:&start
end:&end
contentsEnd:NULL
forRange:range];
NSRange lineRange = NSMakeRange(start, end - start);
NSString *line = [[contents substringWithRange:lineRange] stringByTrimmingCharactersInSet:newlineSet];
if (inParagraph) {
if ([line length] == 0) {
inParagraph = NO;
outputParagraph(output, paragraph);
} else if ([line hasPrefix: @"="] ||
[line hasPrefix: @"*"] ||
[line hasPrefix: @"*"])
{
inParagraph = NO;
outputParagraph(output, paragraph);
outputLine(output, line);
}
else if ([line hasPrefix: @" "] || [line hasPrefix: @" "]) {
outputParagraph(output, paragraph);
paragraph = [[NSMutableString alloc] initWithString:line];
}
else
[paragraph appendString: line];
} else {
if ([line hasPrefix: @" "] || [line hasPrefix: @" "])
{
inParagraph = YES;
paragraph = [[NSMutableString alloc] initWithString:line];
} else {
outputLine(output, line);
}
}
}
if (inParagraph && paragraph && [paragraph length]) {
if ([newlineSet characterIsMember:[contents characterAtIndex:end - 1]])
[output appendFormat:@"%@\n", paragraph];
else
[output appendString:paragraph];
[paragraph release];
paragraph = nil;
}
if ([[self firstTextView] shouldChangeTextInRange:selectedRange
replacementString:output]) {
[textStorage beginEditing];
[textStorage replaceCharactersInRange:selectedRange withString:output];
[[self firstTextView] didChangeText];
[textStorage endEditing];
}
[output release];
}
/* Doesn't check to see if the prev value is the same --- Otherwise the first time doesn't work... */
- (void)updateForRichTextAndRulerState:(BOOL)rich {
NSTextView *view = [self firstTextView];
[view setRichText:rich];
[view setUsesRuler:rich]; // If NO, this correctly gets rid of the ruler if it was up
[view setUsesInspectorBar:rich];
if (!rich && rulerIsBeingDisplayed) [self showRulerDelayed:NO]; // Cancel delayed ruler request
if (rich && ![[self document] isReadOnly]) [self showRulerDelayed:YES];
[view setImportsGraphics:rich];
[[view layoutManager] setUsesScreenFonts:rich ? NO : YES];
}
- (void)configureTypingAttributesAndDefaultParagraphStyleForTextView:(NSTextView *)view {
Document *doc = [self document];
BOOL rich = [doc isRichText];
NSDictionary *textAttributes = [doc defaultTextAttributes:rich];
NSParagraphStyle *paragraphStyle = [textAttributes objectForKey:NSParagraphStyleAttributeName];
[view setTypingAttributes:textAttributes];
[view setDefaultParagraphStyle:paragraphStyle];
}
- (void)convertTextForRichTextState:(BOOL)rich removeAttachments:(BOOL)attachmentFlag {
NSTextView *view = [self firstTextView];
Document *doc = [self document];
NSDictionary *textAttributes = [doc defaultTextAttributes:rich];
NSParagraphStyle *paragraphStyle = [textAttributes objectForKey:NSParagraphStyleAttributeName];
// Note, since the textview content changes (removing attachments and changing attributes) create undo actions inside the textview, we do not execute them here if we're undoing or redoing
if (![[doc undoManager] isUndoing] && ![[doc undoManager] isRedoing]) {
NSTextStorage *textStorage = [[self document] textStorage];
if (!rich && attachmentFlag) [self removeAttachments];
NSRange range = NSMakeRange(0, [textStorage length]);
if ([view shouldChangeTextInRange:range replacementString:nil]) {
[textStorage beginEditing];
[doc applyDefaultTextAttributes:rich];
[textStorage endEditing];
[view didChangeText];
}
}
[view setTypingAttributes:textAttributes];
[view setDefaultParagraphStyle:paragraphStyle];
}
- (NSUInteger)numberOfPages {
return hasMultiplePages ? [[scrollView documentView] numberOfPages] : 1;
}
- (void)updateTextViewGeometry {
MultiplePageView *pagesView = [scrollView documentView];
[[[self layoutManager] textContainers] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[[obj textView] setFrame:[pagesView documentRectForPageNumber:idx]];
}];
}
- (void)setupPagesViewForLayoutOrientation:(NSTextLayoutOrientation)orientation {
MultiplePageView *pagesView = [scrollView documentView];
[pagesView setLayoutOrientation:orientation];
[[self firstTextView] setLayoutOrientation:orientation];
[self updateTextViewGeometry];
[scrollView setHasHorizontalRuler:(NSTextLayoutOrientationHorizontal == orientation) ? YES : NO];
[scrollView setHasVerticalRuler:(NSTextLayoutOrientationHorizontal == orientation) ? NO : YES];
}
- (void)addPage {
NSZone *zone = [self zone];
NSUInteger numberOfPages = [self numberOfPages];
MultiplePageView *pagesView = [scrollView documentView];
NSSize textSize = [pagesView documentSizeInPage];
NSTextContainer *textContainer = [[NSTextContainer allocWithZone:zone] initWithContainerSize:textSize];
NSTextView *textView;
NSUInteger orientation = [pagesView layoutOrientation];
NSRect visibleRect = [pagesView visibleRect];
CGFloat originalWidth = NSWidth([pagesView bounds]);
[textContainer setWidthTracksTextView:YES];
[textContainer setHeightTracksTextView:YES];
[pagesView setNumberOfPages:numberOfPages + 1];
if (NSTextLayoutOrientationVertical == [pagesView layoutOrientation]) {
visibleRect.origin.x += (NSWidth([pagesView bounds]) - originalWidth);
[pagesView scrollRectToVisible:visibleRect];
}
textView = [[NSTextView allocWithZone:zone] initWithFrame:[pagesView documentRectForPageNumber:numberOfPages] textContainer:textContainer];
[textView setLayoutOrientation:orientation];
[textView setHorizontallyResizable:NO];
[textView setVerticallyResizable:NO];
if (NSTextLayoutOrientationVertical == orientation) { // Adjust the initial container size
textSize = NSMakeSize(textSize.height, textSize.width); // Translate size
[textContainer setContainerSize:textSize];
}
[self configureTypingAttributesAndDefaultParagraphStyleForTextView:textView];
[pagesView addSubview:textView];
[[self layoutManager] addTextContainer:textContainer];
[textView release];
[textContainer release];
}
- (void)removePage {
NSUInteger numberOfPages = [self numberOfPages];
NSArray *textContainers = [[self layoutManager] textContainers];
NSTextContainer *lastContainer = [textContainers objectAtIndex:[textContainers count] - 1];
MultiplePageView *pagesView = [scrollView documentView];
[pagesView setNumberOfPages:numberOfPages - 1];
[[lastContainer textView] removeFromSuperview];
[[lastContainer layoutManager] removeTextContainerAtIndex:[textContainers count] - 1];
}
- (NSView *)documentView {
return [scrollView documentView];
}
- (void)setHasMultiplePages:(BOOL)pages force:(BOOL)force {
NSTextLayoutOrientation orientation = NSTextLayoutOrientationHorizontal;
NSZone *zone = [self zone];
if (!force && (hasMultiplePages == pages)) return;
hasMultiplePages = pages;
[[self firstTextView] removeObserver:self forKeyPath:@"backgroundColor"];
[[self firstTextView] unbind:@"editable"];
if ([self firstTextView]) {
orientation = [[self firstTextView] layoutOrientation];
} else {
NSArray *sections = [[self document] originalOrientationSections];
if (([sections count] > 0) && (NSTextLayoutOrientationVertical == [[[sections objectAtIndex:0] objectForKey:NSTextLayoutSectionOrientation] unsignedIntegerValue])) orientation = NSTextLayoutOrientationVertical;
}
if (hasMultiplePages) {
NSTextView *textView = [self firstTextView];
MultiplePageView *pagesView = [[MultiplePageView allocWithZone:zone] init];
[scrollView setDocumentView:pagesView];
[pagesView setPrintInfo:[[self document] printInfo]];
[pagesView setLayoutOrientation:orientation];
// Add the first new page before we remove the old container so we can avoid losing all the shared text view state.
[self addPage];
if (textView) {
[[self layoutManager] removeTextContainerAtIndex:0];
}
if (NSTextLayoutOrientationVertical == orientation) [self updateTextViewGeometry];
[scrollView setHasVerticalScroller:YES];
[scrollView setHasHorizontalScroller:YES];
// Make sure the selected text is shown
[[self firstTextView] scrollRangeToVisible:[[self firstTextView] selectedRange]];
NSRect visRect = [pagesView visibleRect];
NSRect pageRect = [pagesView pageRectForPageNumber:0];
if (visRect.size.width < pageRect.size.width) { // If we can't show the whole page, tweak a little further
NSRect docRect = [pagesView documentRectForPageNumber:0];
if (visRect.size.width >= docRect.size.width) { // Center document area in window
visRect.origin.x = docRect.origin.x - floor((visRect.size.width - docRect.size.width) / 2);
if (visRect.origin.x < pageRect.origin.x) visRect.origin.x = pageRect.origin.x;
} else { // If we can't show the document area, then show left edge of document area (w/out margins)
visRect.origin.x = docRect.origin.x;
}
[pagesView scrollRectToVisible:visRect];
}
[pagesView release];
} else {
NSSize size = [scrollView contentSize];
NSTextContainer *textContainer = [[NSTextContainer allocWithZone:zone] initWithContainerSize:NSMakeSize(size.width, CGFLOAT_MAX)];
NSTextView *textView = [[NSTextView allocWithZone:zone] initWithFrame:NSMakeRect(0.0, 0.0, size.width, size.height) textContainer:textContainer];
// Insert the single container as the first container in the layout manager before removing the existing pages in order to preserve the shared view state.
[[self layoutManager] insertTextContainer:textContainer atIndex:0];
if ([[scrollView documentView] isKindOfClass:[MultiplePageView class]]) {
NSArray *textContainers = [[self layoutManager] textContainers];
NSUInteger cnt = [textContainers count];
while (cnt-- > 1) {
[[self layoutManager] removeTextContainerAtIndex:cnt];
}
}
[textContainer setWidthTracksTextView:YES];
[textContainer setHeightTracksTextView:NO]; /* Not really necessary */
[textView setHorizontallyResizable:NO]; /* Not really necessary */
[textView setVerticallyResizable:YES];
[textView setAutoresizingMask:NSViewWidthSizable];
[textView setMinSize:size]; /* Not really necessary; will be adjusted by the autoresizing... */
[textView setMaxSize:NSMakeSize(CGFLOAT_MAX, CGFLOAT_MAX)]; /* Will be adjusted by the autoresizing... */
[self configureTypingAttributesAndDefaultParagraphStyleForTextView:textView];
[textView setLayoutOrientation:orientation]; // this configures the above settings
/* The next line should cause the multiple page view and everything else to go away */
[scrollView setDocumentView:textView];
[scrollView setHasVerticalScroller:YES];
[scrollView setHasHorizontalScroller:YES];
[textView release];
[textContainer release];
// Show the selected region
[[self firstTextView] scrollRangeToVisible:[[self firstTextView] selectedRange]];
}
[scrollView setHasHorizontalRuler:((orientation == NSTextLayoutOrientationHorizontal) ? YES : NO)];
[scrollView setHasVerticalRuler:((orientation == NSTextLayoutOrientationHorizontal) ? NO : YES)];
[[self firstTextView] addObserver:self forKeyPath:@"backgroundColor" options:0 context:NULL];
[[self firstTextView] bind:@"editable" toObject:self withKeyPath:@"document.readOnly" options:[NSDictionary dictionaryWithObject:NSNegateBooleanTransformerName forKey:NSValueTransformerNameBindingOption]];
[[scrollView window] makeFirstResponder:[self firstTextView]];
[[scrollView window] setInitialFirstResponder:[self firstTextView]]; // So focus won't be stolen (2934918)
}
/* We override these pair of methods so we can stash away the scrollerStyle, since we want to preserve the size of the document (rather than the size of the window).
*/
- (void)restoreStateWithCoder:(NSCoder *)coder {
[super restoreStateWithCoder:coder];
if ([coder containsValueForKey:@"scrollerStyle"]) {
NSScrollerStyle previousScrollerStyle = [coder decodeIntegerForKey:@"scrollerStyle"];
if (previousScrollerStyle != [NSScroller preferredScrollerStyle] && ! [[self document] hasMultiplePages]) {
// When we encoded the frame, the window was sized for this saved style. The preferred scroller style has since changed. Given our current frame and the style it had applied, compute how big the view must have been, and then resize ourselves to make the view that size.
NSSize scrollViewSize = [scrollView frame].size;
NSSize previousViewSize = [[scrollView class] contentSizeForFrameSize:scrollViewSize horizontalScrollerClass:[scrollView hasHorizontalScroller] ? [NSScroller class] : Nil verticalScrollerClass:[scrollView hasVerticalScroller] ? [NSScroller class] : Nil borderType:[scrollView borderType] controlSize:NSRegularControlSize scrollerStyle:previousScrollerStyle];
previousViewSize.width -= (defaultTextPadding() * 2.0);
[self resizeWindowForViewSize:previousViewSize];
}
}
}
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
[super encodeRestorableStateWithCoder:coder];
// Normally you would just encode things that changed; however, since the only invalidation we do is for scrollerStyle, this approach is fine for now.
[coder encodeInteger:[NSScroller preferredScrollerStyle] forKey:@"scrollerStyle"];
}
- (void)resizeWindowForViewSize:(NSSize)size {
NSWindow *window = [self window];
NSRect origWindowFrame = [window frame];
NSScrollerStyle scrollerStyle;
if (![[self document] hasMultiplePages]) {
size.width += (defaultTextPadding() * 2.0);
scrollerStyle = [NSScroller preferredScrollerStyle];
} else {
scrollerStyle = NSScrollerStyleLegacy; // For the wrap-to-page case, which uses legacy style scrollers for now
}
NSRect scrollViewRect = [[window contentView] frame];
scrollViewRect.size = [[scrollView class] frameSizeForContentSize:size horizontalScrollerClass:[scrollView hasHorizontalScroller] ? [NSScroller class] : Nil verticalScrollerClass:[scrollView hasVerticalScroller] ? [NSScroller class] : Nil borderType:[scrollView borderType] controlSize:NSRegularControlSize scrollerStyle:scrollerStyle];
NSRect newFrame = [window frameRectForContentRect:scrollViewRect];
newFrame.origin = NSMakePoint(origWindowFrame.origin.x, NSMaxY(origWindowFrame) - newFrame.size.height);
[window setFrame:newFrame display:YES];
}
- (void)setupWindowForDocument {
NSSize viewSize = [[self document] viewSize];
[self setupTextViewForDocument];
if (!NSEqualSizes(viewSize, NSZeroSize)) { // Document has a custom view size that should be used
[self resizeWindowForViewSize:viewSize];
} else { // Set the window size from defaults...
if (hasMultiplePages) {
[self resizeWindowForViewSize:[[scrollView documentView] pageRectForPageNumber:0].size];
} else {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger windowHeight = [defaults integerForKey:WindowHeight];
NSInteger windowWidth = [defaults integerForKey:WindowWidth];
NSFont *font = [[self document] isRichText] ? [NSFont userFontOfSize:0.0] : [[NSFont userFixedPitchFontOfSize:0.0] screenFontWithRenderingMode:NSFontDefaultRenderingMode];
NSSize size;
size.height = ceil([[self layoutManager] defaultLineHeightForFont:font] * windowHeight);
size.width = [@"x" sizeWithAttributes:[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]].width;
if (size.width == 0.0) size.width = [@" " sizeWithAttributes:[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName]].width; /* try for space width */
if (size.width == 0.0) size.width = [font maximumAdvancement].width; /* or max width */
size.width = ceil(size.width * windowWidth);
[self resizeWindowForViewSize:size];
}
}
}
- (void)windowDidLoad {
[super windowDidLoad];
// This creates the first text view
[self setHasMultiplePages:[[self document] hasMultiplePages] force:YES];
// This sets it up
[self setupInitialTextViewSharedState];
// This makes sure the window's UI (including text view shared state) is updated to reflect the document
[self setupWindowForDocument];
// Changes to the zoom popup need to be communicated to the document
if ([[self document] hasMultiplePages]) [scrollView setScaleFactor:[[self document] scaleFactor] adjustPopup:YES];
[scrollView addObserver:self forKeyPath:@"scaleFactor" options:0 context:NULL];
[[scrollView verticalScroller] addObserver:self forKeyPath:@"scrollerStyle" options:0 context:NULL];
[[[self document] undoManager] removeAllActions];
}
- (void)setDocumentEdited:(BOOL)edited {
[super setDocumentEdited:edited];
if (edited) [[self document] setOriginalOrientationSections:nil];
}
/* Layout orientation sections */
- (NSArray *)layoutOrientationSections {
NSArray *textContainers = [layoutMgr textContainers];
NSMutableArray *sections = nil;
NSUInteger layoutOrientation = 0; // horizontal
NSRange range = NSMakeRange(0, 0);
for (NSTextContainer *container in textContainers) {
NSUInteger newOrientation = [container layoutOrientation];
if (newOrientation != layoutOrientation) {
if (range.length > 0) {
if (!sections) sections = [NSMutableArray arrayWithCapacity:0];
[sections addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInteger:layoutOrientation], NSTextLayoutSectionOrientation, [NSValue valueWithRange:range], NSTextLayoutSectionRange, nil]];
range.length = 0;
}
layoutOrientation = newOrientation;
}
if (layoutOrientation > 0) {
NSRange containerRange = [layoutMgr characterRangeForGlyphRange:[layoutMgr glyphRangeForTextContainer:container] actualGlyphRange:NULL];
if (range.length == 0) {
range = containerRange;
} else {
range.length = NSMaxRange(containerRange) - range.location;
}
}
}
if (range.length > 0) {
if (!sections) sections = [NSMutableArray arrayWithCapacity:0];
[sections addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInteger:layoutOrientation], NSTextLayoutSectionOrientation, [NSValue valueWithRange:range], NSTextLayoutSectionRange, nil]];
}
return sections;
}
- (void)toggleRichWithNewFileType:(NSString *)type {
Document *document = [self document];
NSURL *fileURL = [document fileURL];
BOOL isRich = [document isRichText]; // This is the old value
NSUndoManager *undoManager = [document undoManager];
[undoManager beginUndoGrouping];
NSString *undoType = (NSString *)((isRich) ? (([[[self firstTextView] textStorage] containsAttachments] || [[document fileType] isEqualToString:(NSString *)kUTTypeRTFD]) ? kUTTypeRTFD : kUTTypeRTF) : kUTTypePlainText);
[undoManager registerUndoWithTarget:self selector:_cmd object:undoType];
[document setUsesScreenFonts:isRich];
[self updateForRichTextAndRulerState:!isRich];
[self convertTextForRichTextState:!isRich removeAttachments:isRich];
if (isRich) {
[document clearDocumentProperties];
} else {
[document setDocumentPropertiesToDefaults];
}
[undoManager setActionName:([undoManager isUndoing] ^ isRich) ? NSLocalizedString(@"Make Plain Text", @"Undo menu item text (without 'Undo ') for making a document plain text") : NSLocalizedString(@"Make Rich Text", @"Undo menu item text (without 'Undo ') for making a document rich text")];
[undoManager endUndoGrouping];
if (type == nil) type = (NSString *)(isRich ? kUTTypePlainText : kUTTypeRTF);
if (fileURL) {
// Synchronously calling a method that invokes -performAsynchronousFileAccessUsingBlock: (like -saveToURL:ofType:forSaveOperation:completionHandler:) in the completion handler of method that uses -continueAsynchronousWorkOnMainThreadUsingBlock: to invoke its completion handler on the main thread (like -autosaveWithImplicitCancellability:completionHandler:, used in -toggleRich:) triggers a bug in NSdocument where the -performAsynchronousFileAccessUsingBlock: invocation has no effect besides invoking its block. This can potentially result in two 'file access' blocks being invoked at the same time, which breaks the contract.
CFRunLoopPerformBlock(CFRunLoopGetMain(), kCFRunLoopDefaultMode, ^{
[document saveToURL:fileURL ofType:type forSaveOperation:NSAutosaveInPlaceOperation completionHandler:^(NSError *error) {
if (error) {
[document setFileURL:nil];
[document setFileType:type];
}
}];
});
} else {
[document setFileType:type];
}
}
/* toggleRich: puts up an alert before ultimately calling -setRichText:
*/
- (void)toggleRich:(id)sender {
Document *document = [self document];
[document performActivityWithSynchronousWaiting:YES usingBlock:^(void (^activityCompletionHandler)(void)) {
// What we'll do to cause the document to toggle rich, after maybe showing an alert about loss of information.
void (^continueTogglingRich)(void) = ^{
[document autosaveWithImplicitCancellability:NO completionHandler:^(NSError *error) {
if (!error) {
[self toggleRichWithNewFileType:nil];
}
activityCompletionHandler();
}];
};
// Check if there is any loss of information, waiting for any previous saves (which might be changing the type) to complete.
__block BOOL willLoseInformation = NO;
[document performSynchronousFileAccessUsingBlock:^{
willLoseInformation = [document toggleRichWillLoseInformation];
}];
if (willLoseInformation) {
NSString *messageText = NSLocalizedString(@"Convert this document to plain text?", @"Title of alert confirming Make Plain Text");
NSString *defaultButton = NSLocalizedString(@"OK", @"OK");
NSString *alternateButton = NSLocalizedString(@"Cancel", @"Button choice that allows the user to cancel.");
NSString *informativeText = NSLocalizedString(@"Making a rich text document plain will lose all text styles (such as fonts and colors), images, attachments, and document properties.", @"Subtitle of alert confirming Make Plain Text");
NSAlert *alert = [NSAlert alertWithMessageText:messageText defaultButton:defaultButton alternateButton:alternateButton otherButton:nil informativeTextWithFormat:@"%@", informativeText];
[alert beginSheetModalForWindow:[[self document] windowForSheet] modalDelegate:self didEndSelector:@selector(didEndToggleRichSheet:returnCode:contextInfo:) contextInfo:Block_copy(^(BOOL okToToggleRich){
if (okToToggleRich) {
continueTogglingRich();
} else {
activityCompletionHandler();
}
})];
} else {
continueTogglingRich();
}
}];
}
- (void)didEndToggleRichSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void (^)(BOOL))block {
block(returnCode == NSAlertDefaultReturn);
Block_release(block);
}
/* Layout orientation
*/
- (void)toggleLayoutOrientation:(id)sender {
NSInteger tag = [sender tag];
if (hasMultiplePages) {
NSUInteger count = [[[self layoutManager] textContainers] count];
while (count-- > 1) [self removePage]; // remove 2nd ~ nth pages
[self setupPagesViewForLayoutOrientation:tag];
} else {
[[self firstTextView] setLayoutOrientation:[sender tag]];
}
}
@end
@implementation DocumentWindowController(Delegation)
/* Window delegation messages */
- (NSRect)windowWillUseStandardFrame:(NSWindow *)window defaultFrame:(NSRect)defaultFrame {
if (!hasMultiplePages) { // If not wrap-to-page, use the default suggested
return defaultFrame;
} else {
NSRect currentFrame = [window frame]; // Get the current size and location of the window
NSRect standardFrame;
NSSize paperSize = [[[self document] printInfo] paperSize]; // Get a frame size that fits the current printable page
NSRect newScrollView;
// Get a frame for the window content, which is a scrollView
newScrollView.origin = NSZeroPoint;
newScrollView.size = [[scrollView class] frameSizeForContentSize:paperSize horizontalScrollerClass:[scrollView hasHorizontalScroller] ? [NSScroller class] : Nil verticalScrollerClass:[scrollView hasVerticalScroller] ? [NSScroller class] : Nil borderType:[scrollView borderType] controlSize:NSRegularControlSize scrollerStyle:NSScrollerStyleLegacy];
// The standard frame for the window is now the frame that will fit the scrollView content
standardFrame.size = [[window class] frameRectForContentRect:newScrollView styleMask:[window styleMask]].size;
// Set the top left of the standard frame to be the same as that of the current window
standardFrame.origin.y = NSMaxY(currentFrame) - standardFrame.size.height;
standardFrame.origin.x = currentFrame.origin.x;
return standardFrame;
}
}
- (void)windowDidResize:(NSNotification *)notification {
[[self document] setTransient:NO]; // Since the user has taken an interest in the window, clear the document's transient status