-
Notifications
You must be signed in to change notification settings - Fork 0
/
Document.m
340 lines (295 loc) · 7.52 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
/* All rights reserved */
#include <AppKit/AppKit.h>
#import "Document.h"
#import "Rtf.h"
#import "Util.h"
#import "TextView.h"
#import "Editor.h"
@implementation Document
- (NSString *) windowNibName
{
return @"Document.gorm";
}
- (id) init
{
self = [super init];
return self;
}
- (BOOL) readFromData:(NSData *) data
ofType:(NSString *) type
error:(NSError *) error
{
return YES;
}
- (void) registerPasteboard
{
[buffer registerForDraggedTypes:[NSArray arrayWithObjects:
NSFilenamesPboardType
,NSURLPboardType
,NSDragPboard
,NSStringPboardType
,NSRTFDPboardType
,NSRTFPboardType
,nil]];
[buffer draggingSourceOperationMaskForLocal:
NSDragOperationCopy | NSDragOperationLink
| NSDragOperationGeneric];
}
- (BOOL) readFromURL:(NSURL *) url
ofType:(NSString *) type
error:(NSError *) error
{
Util *util = [[Util alloc] init];
NSArray *parsedFile = [util parseFile: [url path]];
fileToOpen = [parsedFile objectAtIndex:0];
fileToOpen = [util validateFile:fileToOpen];
argument = [parsedFile objectAtIndex:1];
[util release];
if (fileToOpen == nil)
return NO;
[self setDocumentPath:fileToOpen];
return YES;
}
- (NSString *) loadContentFromFile: (NSString *)file
{
Util *util = [[Util alloc] init];
file = [util validateFile: file];
[util release];
if (file == nil)
return nil;
NSError *contentError = 0;
NSString *content = [NSString
stringWithContentsOfFile:file
encoding:NSUTF8StringEncoding
error:&contentError];
if (contentError != nil) {
contentError = 0;
[NSString
stringWithContentsOfFile:file
encoding:NSNEXTSTEPStringEncoding
error:&contentError];
}
if (contentError != nil) {
return nil;
}
return content;
}
- (BOOL) loadDataRepresentation:(NSData *)data ofType:(NSString *)type
{
if ([type isEqualToString:@"Rich Text"]) {
NSData *newBufferContent = [[[NSData alloc] initWithData:
data]
autorelease];
NSAttributedString *newBufferContentStr =
[[[NSAttributedString alloc] initWithRTF:
newBufferContent documentAttributes:NULL]
autorelease];
stringRTFContent = [[NSMutableAttributedString alloc]
init];
[stringRTFContent appendAttributedString: newBufferContentStr];
} else {
stringContent = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
if (stringContent == nil) {
stringContent = [[[NSString alloc] initWithData:data
encoding:NSNEXTSTEPStringEncoding]
autorelease];
}
}
return YES;
}
- (void) awakeFromNib
{
if (stringContent != nil) {
[[buffer textStorage] beginEditing];
[buffer setString: stringContent];
[[buffer textStorage] endEditing];
[stringContent release];
stringContent = nil;
}
if (stringRTFContent != nil) {
[[buffer textStorage] beginEditing];
[[buffer textStorage] setAttributedString: stringRTFContent];
[[buffer textStorage] endEditing];
[stringRTFContent release];
stringRTFContent = nil;
}
Util *util = [[Util alloc] init];
if (fileToOpen != nil) {
Rtf *rtf = [[Rtf alloc] init];
[self setDocumentPath: fileToOpen];
if ([[util getFileExtension:fileToOpen] isEqualToString:@"rtf"]) {
[rtf loadDocumentRtf:[self getDocumentPath] view:buffer];
} else {
NSString *content = [self
loadContentFromFile:fileToOpen];
if (content != nil) {
[buffer setString:content];
}
}
[rtf release];
}
if (![argument isEqualToString:@""]) {
[util findContent:self content:argument];
}
[util release];
if ([self getDocumentPath] != nil) {
[[buffer window] setTitle: [self getDocumentPath]];
}
[[buffer window] makeFirstResponder:buffer];
[[buffer window] setDocumentEdited:NO];
[self registerPasteboard];
}
- (void) windowControllerDidLoadNib: (NSWindowController *) controller
{
[buffer centerSelectionInVisibleArea:nil];
[[buffer window] setTitle: [self getDocumentPath]];
[self registerPasteboard];
[[buffer window] makeFirstResponder:buffer];
}
- (void) openDocument: (id)sender
{
NSOpenPanel *openDialog = [NSOpenPanel openPanel];
[openDialog setAllowsMultipleSelection:YES];
[openDialog runModal];
NSArray *fileNames = [openDialog filenames];
int i;
Util *util = [[Util alloc] init];
for (i = 0; i < [fileNames count]; ++i) {
NSString *filePath = [fileNames objectAtIndex:i];
filePath = [util validateFile:filePath];
if (filePath != nil) {
id d = [[NSDocumentController sharedDocumentController]
openDocumentWithContentsOfFile:filePath
display:YES];
[d setDocumentPath: filePath];
}
}
[util release];
}
- (void) saveDocument: (id)sender
{
//[buffer breakUndoCoalescing];
if ([self getDocumentPath] != nil) {
Rtf *rtf = [[Rtf alloc] init];
Util *util = [[Util alloc] init];
if (![[util getFileExtension: [self getDocumentPath]] isEqualToString:@"rtf"]) {
NSString *content = [buffer string];
[content writeToFile:[self getDocumentPath] atomically:YES];
[[buffer window] setTitle: [self getDocumentPath]];
} else {
[rtf saveDocumentRtf:[self getDocumentPath]
view:buffer];
}
[rtf release];
[util release];
} else {
[self saveDocumentAs:self];
}
[buffer setDocumentEdited:NO];
}
- (void) saveDocumentAs: (id)sender
{
// [buffer breakUndoCoalescing];
NSSavePanel *openDialog = [NSSavePanel savePanel];
[openDialog setCanCreateDirectories: YES];
[openDialog runModal];
NSURL *res = [openDialog URL];
NSString *path = [res pathWithEscapes];
NSError *fileError = 0;
NSFileManager *fm = [NSFileManager defaultManager];
NSDictionary *dict = [fm attributesOfItemAtPath:path
error:&fileError];
if ([dict fileType] == NSFileTypeDirectory)
return;
Rtf *rtf = [[Rtf alloc] init];
Util *util = [[Util alloc] init];
if (![[util getFileExtension: [self getDocumentPath]] isEqualToString:@"rtf"]) {
NSString *content = [buffer string];
[content writeToFile:path atomically:YES];
} else {
[rtf saveDocumentRtf:path view:buffer];
}
[rtf release];
[util release];
[self setDocumentPath:path];
[[buffer window] setTitle: [self getDocumentPath]];
[buffer setDocumentEdited:NO];
}
- (void) saved: (id)sender
{
if ([self getDocumentPath] != nil) {
NSString *content = [self loadContentFromFile:[self getDocumentPath]];
if (content != nil) {
[[buffer textStorage] beginEditing];
[buffer setString: content];
[[buffer textStorage] endEditing];
[buffer setDocumentEdited:NO];
}
} else {
NSRunAlertPanel(_(@"Error"),
_(@"Error: Document is not saved."),
_(@"OK"), nil, nil);
}
}
- (void) revertDocumentToSaved: (id)sender
{
[self saved:sender];
}
- (BOOL)isDocumentEdited
{
return [buffer isDocumentEdited];
}
- (id) getBuffer
{
return buffer;
}
- (void)setDocumentPath:(NSString *)path
{
[[buffer window] setRepresentedFilename: path];
}
- (NSString *)getDocumentPath
{
return [[buffer window] representedFilename];
}
- (void) name: (id)sender
{
NSString *name = [self getDocumentPath];
if (name == nil) {
NSRunAlertPanel(_(@"Error"),
_(@"Error: Document have no name."),
_(@"OK"), nil, nil);
return;
}
NSPasteboard *pboard = [NSPasteboard generalPasteboard];
[pboard declareTypes: [NSArray arrayWithObjects: NSStringPboardType
,NSFilenamesPboardType, nil]
owner: nil];
[pboard setString: name forType:NSStringPboardType];
[pboard setString: name forType:NSFilenamesPboardType];
}
- (void) showRegisters: (id)sender
{
[editor showRegisters:sender];
}
- (void) getRegister: (id)sender
{
[editor getRegister:sender];
}
- (void) setRegister: (id)sender
{
[editor setRegister:sender];
}
- (void) showLocations: (id)sender
{
[editor showLocations:sender];
}
- (void) setLocation: (id)sender
{
[editor setLocation:sender];
}
- (void) goToLocation: (id)sender
{
[editor goToLocation:sender];
}
@end