Skip to content

Commit 76ada98

Browse files
author
Hans-Jörg Bibiko
committed
• further work on Rd file support
- some improvement for saving files, esp. for changing file types from R to Rd e.g. • corrected misspelled 'Secction' git-svn-id: https://svn.r-project.org/R-packages/trunk/Mac-GUI@5997 694ef91d-65df-0310-b7bb-92e67a308ead
1 parent e9d894a commit 76ada98

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

RDocument.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
BOOL isEditable; // determines whether this document can be edited
4343
BOOL isREdit; // set to YES by R_Edit to exit modal state on close
44+
BOOL fileTypeWasChangedWhileSaving;
4445

4546
NSPopUpButton *encodingPopUp;
4647

RDocument.m

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ - (id)init
5656
isEditable=YES;
5757
isREdit=NO;
5858
myWinCtrl=nil;
59+
fileTypeWasChangedWhileSaving = NO;
5960
}
6061
return self;
6162
}
@@ -120,9 +121,24 @@ - (void) setFileEncoding: (int) encoding
120121

121122
- (void)didSaveSelector
122123
{
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+
123138
// Remain focus on current document after closing SaveAs panel
124139
[[myWinCtrl window] makeKeyWindow];
125140
encodingPopUp = nil;
141+
126142
}
127143

128144
- (void)runModalSavePanelForSaveOperation:(NSSaveOperationType)saveOperation delegate:(id)delegate didSaveSelector:(SEL)didSaveSelector contextInfo:(void *)contextInfo
@@ -144,6 +160,15 @@ - (BOOL)prepareSavePanel:(NSSavePanel *)savePanel
144160
if(encodingPopUp) [encodingPopUp setEnabled:YES];
145161
[savePanel setAllowsOtherFileTypes:YES];
146162
}
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+
}
147172
else if(initialContentsType && [initialContentsType hasSuffix:@".rtf"]) {
148173
[savePanel setAllowedFileTypes:[NSArray arrayWithObject:@"rtf"]];
149174
[savePanel setAllowsOtherFileTypes:NO];
@@ -156,19 +181,35 @@ - (BOOL)prepareSavePanel:(NSSavePanel *)savePanel
156181

157182
}
158183

159-
- (BOOL)writeToFile:(NSString *)fileName ofType:(NSString *)docType {
184+
- (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)docType error:(NSError **)outError
185+
{
160186

161187
SLog(@"RDocument.writeToFile: %@ ofType: %@ ", fileName, docType);
162188

163-
if([[fileName lowercaseString] hasSuffix:@".rtf"]) {
189+
NSString *oldFileType = (initialContentsType)?:ftRSource;
190+
191+
if([[[absoluteURL absoluteString] lowercaseString] hasSuffix:@".rtf"]) {
164192
SLog(@" - docType was changed to rtf due to file extension");
165193
if(initialContentsType) [initialContentsType release], initialContentsType = nil;
166194
initialContentsType = [[NSString stringWithString:@"public.rtf"] retain];
167195
}
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+
}
168207

169208
SLog(@" - used docType %@", (initialContentsType)?:ftRSource);
170209

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];
172213

173214
}
174215

RdEditorToolbar.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ @implementation RdEditorToolbar
9292
tiSecList = [[NSToolbarItem alloc] initWithItemIdentifier: RDETI_SecList];
9393
[tiSecList setLabel:NLS(@"Sections")];
9494
[tiSecList setPaletteLabel:NLS(@"Sections")];
95-
[tiSecList setToolTip:NLS(@"List of Secctions")];
95+
[tiSecList setToolTip:NLS(@"List of Sections")];
9696
SLog(@" - tiSecList=%@, view=%@", tiSecList, myView);
9797
[tiSecList setView:myView];
9898
[tiSecList setMinSize:NSMakeSize(100,NSHeight([myView frame]))];

0 commit comments

Comments
 (0)