@@ -56,6 +56,7 @@ - (id)init
56
56
isEditable=YES ;
57
57
isREdit=NO ;
58
58
myWinCtrl=nil ;
59
+ fileTypeWasChangedWhileSaving = NO ;
59
60
}
60
61
return self;
61
62
}
@@ -120,9 +121,24 @@ - (void) setFileEncoding: (int) encoding
120
121
121
122
- (void )didSaveSelector
122
123
{
124
+
125
+ // Reopen file if file type was changed while saving
126
+ if (fileTypeWasChangedWhileSaving) {
127
+ NSError *theError = nil ;
128
+ fileTypeWasChangedWhileSaving = NO ;
129
+ [myWinCtrl close ];
130
+ [[NSDocumentController sharedDocumentController ] openDocumentWithContentsOfURL: [self fileURL ] display: YES error: &theError];
131
+ if (theError) {
132
+ NSLog (@" *** openDocumentWithContentsOfURL: failed with %@ " , theError);
133
+ NSBeep ();
134
+ }
135
+ return ;
136
+ }
137
+
123
138
// Remain focus on current document after closing SaveAs panel
124
139
[[myWinCtrl window ] makeKeyWindow ];
125
140
encodingPopUp = nil ;
141
+
126
142
}
127
143
128
144
- (void )runModalSavePanelForSaveOperation : (NSSaveOperationType )saveOperation delegate : (id )delegate didSaveSelector : (SEL )didSaveSelector contextInfo : (void *)contextInfo
@@ -144,6 +160,15 @@ - (BOOL)prepareSavePanel:(NSSavePanel *)savePanel
144
160
if (encodingPopUp) [encodingPopUp setEnabled: YES ];
145
161
[savePanel setAllowsOtherFileTypes: YES ];
146
162
}
163
+ else if (initialContentsType && [initialContentsType isEqualToString: ftRdDoc]) {
164
+ [savePanel setAllowedFileTypes: [NSArray arrayWithObject: @" Rd" ]];
165
+ if (myWinCtrl)
166
+ [savePanel setAccessoryView: [[[NSDocumentController sharedDocumentController ] class ] encodingAccessory: (NSStringEncoding )documentEncoding
167
+ includeDefaultEntry: NO
168
+ encodingPopUp: &encodingPopUp]];
169
+ if (encodingPopUp) [encodingPopUp setEnabled: YES ];
170
+ [savePanel setAllowsOtherFileTypes: YES ];
171
+ }
147
172
else if (initialContentsType && [initialContentsType hasSuffix: @" .rtf" ]) {
148
173
[savePanel setAllowedFileTypes: [NSArray arrayWithObject: @" rtf" ]];
149
174
[savePanel setAllowsOtherFileTypes: NO ];
@@ -156,19 +181,35 @@ - (BOOL)prepareSavePanel:(NSSavePanel *)savePanel
156
181
157
182
}
158
183
159
- - (BOOL )writeToFile : (NSString *)fileName ofType : (NSString *)docType {
184
+ - (BOOL )writeToURL : (NSURL *)absoluteURL ofType : (NSString *)docType error : (NSError **)outError
185
+ {
160
186
161
187
SLog (@" RDocument.writeToFile: %@ ofType: %@ " , fileName, docType);
162
188
163
- if ([[fileName lowercaseString ] hasSuffix: @" .rtf" ]) {
189
+ NSString *oldFileType = (initialContentsType)?:ftRSource;
190
+
191
+ if ([[[absoluteURL absoluteString ] lowercaseString ] hasSuffix: @" .rtf" ]) {
164
192
SLog (@" - docType was changed to rtf due to file extension" );
165
193
if (initialContentsType) [initialContentsType release ], initialContentsType = nil ;
166
194
initialContentsType = [[NSString stringWithString: @" public.rtf" ] retain ];
167
195
}
196
+ else if ([[[absoluteURL absoluteString ] lowercaseString ] hasSuffix: @" .rd" ]) {
197
+ SLog (@" - docType was changed to Rd due to file extension" );
198
+ if (initialContentsType) [initialContentsType release ], initialContentsType = nil ;
199
+ initialContentsType = [[NSString stringWithString: ftRdDoc] retain ];
200
+ [self setFileType: ftRdDoc];
201
+ }
202
+ else {
203
+ if (initialContentsType) [initialContentsType release ], initialContentsType = nil ;
204
+ initialContentsType = [[NSString stringWithString: ftRSource] retain ];
205
+ [self setFileType: ftRSource];
206
+ }
168
207
169
208
SLog (@" - used docType %@ " , (initialContentsType)?:ftRSource);
170
209
171
- return [super writeToFile: fileName ofType: (initialContentsType)?:ftRSource];
210
+ fileTypeWasChangedWhileSaving = ([initialContentsType isEqualToString: oldFileType]) ? NO : YES ;
211
+
212
+ return [super writeToURL: absoluteURL ofType: (initialContentsType)?:ftRSource error: outError];
172
213
173
214
}
174
215
0 commit comments