-
Notifications
You must be signed in to change notification settings - Fork 7
/
RDocumentController.m
306 lines (251 loc) · 9.72 KB
/
RDocumentController.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
/*
* R.app : a Cocoa front end to: "R A Computer Language for Statistical Data Analysis"
*
* R.app Copyright notes:
* Copyright (C) 2004-11 The R Foundation
* written by Stefano M. Iacus and Simon Urbanek
* RDocumentController written by Rob Goedman
*
*
* R Copyright notes:
* Copyright (C) 1995-1996 Robert Gentleman and Ross Ihaka
* Copyright (C) 1998-2001 The R Development Core Team
* Copyright (C) 2002-2004 The R Foundation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* A copy of the GNU General Public License is available via WWW at
* http://www.gnu.org/copyleft/gpl.html. You can also obtain it by
* writing to the Free Software Foundation, Inc., 59 Temple Place,
* Suite 330, Boston, MA 02111-1307 USA.
*/
#import "RGUI.h"
#import "RDocumentController.h"
#import "RController.h"
#import "Preferences.h"
#import "PreferenceKeys.h"
#import "QuartzCocoaDocument.h"
#import "RChooseEncodingPopupAccessory.h"
// default autosave is 3 minutes
#define defaultAutosavingDelay (3 * 60.0)
// R defines "error" which is deadly as we use open ... with ... error: where error then gets replaced by Rf_error
#ifdef error
#undef error
#endif
@implementation OpenSaveAccessoryOwner
@end
@implementation RDocumentController
- (id)init {
self = [super init];
SLog(@"RDocumentController%@.init", self);
if(self) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowWillCloseNotifications:)
name:NSWindowWillCloseNotification
object:nil];
[self updatePreferences];
[[Preferences sharedPreferences] addDependent:self];
activeFileType = ftRSource;
}
return self;
}
- (void) dealloc {
SLog(@"RDocumentController%@.dealloc", self);
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (void) updatePreferences
{
BOOL autosaveEnabled = [Preferences flagForKey:kEditorAutosaveKey withDefault:YES];
[self setAutosavingDelay: autosaveEnabled ? defaultAutosavingDelay : 0.0];
SLog(@"%@ autosave %@", self, autosaveEnabled ? @"ENABLED" : @"DISABLED");
}
- (void)windowWillCloseNotifications:(NSNotification*) aNotification
{
NSWindow *w = [aNotification object];
if (w && (
[[(NSObject*)[w delegate] className] isEqualToString:@"RDocumentWinCtrl"]
|| [[(NSObject*)[w delegate] className] isEqualToString:@"QuartzCocoaView"])
) {
SLog(@"RDocumentController%@.windowWillCloseNotifications:%@", self, w);
RDocument *d = [self documentForWindow:w];
SLog(@" - document for window: %@", d);
// if no document is associated wit this window, check whether this is a Quartz Cocoa window
if (!d && [[(NSObject*)[w delegate] className] isEqualToString:@"QuartzCocoaView"]) {
d = (RDocument*)[[QuartzCocoaDocument alloc] initWithWindow:w];
[d makeWindowControllers];
[[NSDocumentController sharedDocumentController] addDocument:d];
[d release];
SLog(@" - added dummy Quartz Cocoa window document");
}
d = [self documentForWindow:w];
SLog(@" - document:%@ of type %@", d, [d fileType]);
// make the next window of the same docType the key window and order it out;
// if no window of the same docType is found make the RConsole the key window
NSWindow *nextWindow = [self findNextWindowForDocType:[d fileType]];
// if document hasREditFlag set focus back to RConsole AND if sanety check fails
BOOL reditcheck = ([[self currentDocument] respondsToSelector:@selector(hasREditFlag)]);
if((!reditcheck || (reditcheck && ![[self currentDocument] hasREditFlag]))
&& nextWindow && nextWindow != w) {
SLog(@" - makeKeyWindow with title %@ and type %@", [nextWindow title], [d fileType]);
} else {
SLog(@" - makeKeyWindow RConsole");
nextWindow = [[RController sharedController] window];
}
[NSApp removeWindowsItem: w];
[nextWindow makeKeyAndOrderFront:nil];
}
}
- (NSWindow*)findLastWindowForDocType:(NSString*)aType
{
return [self findWindowForDocType:aType getLast:YES];
}
- (NSWindow*)findNextWindowForDocType:(NSString*)aType
{
return [self findWindowForDocType:aType getLast:NO];
}
- (NSWindow*)findWindowForDocType:(NSString*)aType getLast:(BOOL)getLast;
{
SLog(@"RDocumentController%@.findWindowForDocType: %@ getLast: %d", self, aType, getLast);
BOOL getLastOrg = getLast;
NSArray *appWindows = [NSApp orderedWindows]; // Get all windows
int i;
Class searchClass = nil;
// set search class due to window's delegates
if([aType isEqualToString:ftRSource])
searchClass = NSClassFromString(@"RDocumentWinCtrl");
else if([aType isEqualToString:ftQuartz])
searchClass = NSClassFromString(@"QuartzCocoaView");
// bail by returning main window if no search class defined
if(!searchClass) {
SLog(@" - passed docType unknown, return console window");
return [[RController sharedController] window];
}
// loop through windows to find first/next window for desired docType
for(i=0; i<[appWindows count]; i++) {
id win = [appWindows objectAtIndex:i];
if([win isVisible] && [[win delegate] isKindOfClass:searchClass]) {
if(getLast) {
SLog(@" - found window with title '%@'", [win title]);
return win;
}
getLast = YES;
}
}
// if no window was found for docType
// try to get the last window
getLast = getLastOrg;
for(i=0; i<[appWindows count]; i++) {
id win = [appWindows objectAtIndex:i];
if([win isVisible]) {
if(getLast) {
SLog(@" - found window with title '%@'", [win title]);
return win;
}
getLast = YES;
}
}
// bail by returning main window
SLog(@" - no window found for passed docType, return console window");
return [[RController sharedController] window];
}
- (NSString *)defaultType
{
return activeFileType;
}
- (IBAction)newRdDocument:(id)sender
{
BOOL useInternalEditor = [Preferences flagForKey:internalOrExternalKey withDefault: YES];
if (!useInternalEditor) {
[self invokeExternalForFile: @""];
return;
}
activeFileType = ftRdDoc;
[super newDocument:sender];
}
- (IBAction)newDocument:(id)sender {
BOOL useInternalEditor = [Preferences flagForKey:internalOrExternalKey withDefault: YES];
if (!useInternalEditor) {
[self invokeExternalForFile: @""];
return;
}
activeFileType = ftRSource;
[super newDocument:sender];
}
- (void)noteNewRecentDocument:(id)doc
{
// suppress showing of doc in recent files if doc was called via REdit
if([[RController sharedController] isREditMode]) return;
[super noteNewRecentDocument:doc];
}
- (Class)documentClassForType:(NSString *)documentTypeName
{
return [RDocument class];
}
- (id) openDocumentWithContentsOfURL:(NSURL *)absoluteURL display:(BOOL)displayDocument error:(NSError **)theError {
if (absoluteURL == nil) {
SLog(@"RDocumentController.openDocumentWithContentsOfURL with null URL. Nothing to do.");
return nil;
}
NSString *aFile = [[absoluteURL path] stringByExpandingTildeInPath];
SLog(@"RDocumentController.openDocumentWithContentsOfURL: %@", aFile);
int res = [[RController sharedController] isImageData: aFile];
if (res == 0 ) {
SLog(@" - detected save image, invoking load instead of the editor");
[[RController sharedController] sendInput: [NSString stringWithFormat:@"load(\"%@\")", aFile]];
return nil;
}
BOOL useInternalEditor = [Preferences flagForKey:internalOrExternalKey withDefault: YES];
if (!useInternalEditor) {
SLog(@" - external editor is enabled, passing over to invokeExternalForFile");
[self invokeExternalForFile:aFile];
return nil;
}
SLog(@" - call super -> openDocumentWithContentsOfURL: %@", aFile);
RDocument *doc = [super openDocumentWithContentsOfURL:absoluteURL display:displayDocument error:theError];
return doc;
}
- (void) invokeExternalForFile:(NSString*)aFile
{
NSString *externalEditor = [NSString stringWithFormat:@"\"%@\"", [Preferences stringForKey:externalEditorNameKey withDefault: @"TextEdit"]];
NSString *cmd;
BOOL editorIsApp = [Preferences flagForKey:appOrCommandKey withDefault: YES];
if (!aFile) aFile=@"";
if (editorIsApp) {
cmd = [@"open -a " stringByAppendingString:externalEditor];
if (![aFile isEqualToString:@""])
cmd = [cmd stringByAppendingString: [NSString stringWithFormat:@" \"%@\"", aFile]];
} else {
cmd = externalEditor;
if (![aFile isEqualToString:@""])
cmd = [cmd stringByAppendingString: [NSString stringWithString: [NSString stringWithFormat:@" \"%@\"", aFile]]];
}
SLog(@" - call external: \"%@\"", cmd);
system([cmd UTF8String]);
}
/**
* Loads the "encoding" accessory view used in save panels.
*/
+ (NSView *)encodingAccessory:(NSStringEncoding)encoding includeDefaultEntry:(BOOL)includeDefaultItem encodingPopUp:(NSPopUpButton **)popup
{
OpenSaveAccessoryOwner *owner = [[[OpenSaveAccessoryOwner alloc] init] autorelease];
// Rather than caching, load the accessory view everytime, as it might appear in multiple panels simultaneously.
if (![NSBundle loadNibNamed:@"EncodingPopupView" owner:owner]) {
NSLog(@"Failed to load EncodingPopupView.xib");
NSBeep();
return nil;
}
if (popup) *popup = owner->encodingPopUp;
[[RChooseEncodingPopupAccessory sharedInstance] setupPopUpCell:[owner->encodingPopUp cell] selectedEncoding:encoding withDefaultEntry:includeDefaultItem];
[owner->label setStringValue:[NSString stringWithFormat:@"%@:", NLS(@"Encoding")]];
return [owner->accessoryView autorelease];
}
@end