-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocument.m
More file actions
129 lines (109 loc) · 3.07 KB
/
Document.m
File metadata and controls
129 lines (109 loc) · 3.07 KB
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
#include <AppKit/AppKit.h>
#include "Document.h"
@implementation Document
- (Document *) initWithView: (NSTabView *) view
{
tabView = view;
tabViewItem = [[NSTabViewItem alloc] initWithIdentifier: self];
[tabViewItem setLabel: @"New File"];
NSScrollView *scrollView = [[NSScrollView alloc] init];
[scrollView setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable ];
[scrollView setHasHorizontalScroller: NO];
[scrollView setHasVerticalScroller: YES];
[tabViewItem setView: scrollView];
textView = [[NSTextView alloc] initWithFrame: [scrollView frame]];
[textView setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable];
[textView setVerticallyResizable: YES];
[textView setHorizontallyResizable: YES];
[[textView textContainer] setWidthTracksTextView: NO];
[textView setHorizontallyResizable: NO];
[textView setVerticallyResizable: YES];
[textView setFont: [[NSApp delegate] font]];
[textView setDelegate: self];
[scrollView setDocumentView: textView];
[tabView addTabViewItem: tabViewItem];
[tabViewItem setInitialFirstResponder: textView];
[tabView selectLastTabViewItem: self];
edited = NO;
return self;
}
- (void) loadFromFile: (NSString *) file
{
NSString *aString = [NSString stringWithContentsOfFile: file];
[textView setString: aString];
fileName = file;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *cleanFileName = [fileManager displayNameAtPath: fileName];
[tabViewItem setLabel: cleanFileName];
}
- (BOOL) save
{
if ([[textView string] writeToFile: fileName atomically: YES])
{
edited = NO;
return YES;
}
else
return NO;
}
- (BOOL) saveAs: (NSString *) file;
{
if ([[textView string] writeToFile: file atomically: YES])
{
fileName = file;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *cleanFileName = [fileManager displayNameAtPath: fileName];
[tabViewItem setLabel: cleanFileName];
edited = NO;
return YES;
}
else
return NO;
}
- (BOOL) close
{
if (edited)
{
int r = NSRunInformationalAlertPanel([NSString stringWithFormat: @"The document '%@' has changed!", (NSString *)[tabViewItem label]], @"Save changes?", @"Save", @"Cancel", @"Don't Save");
switch (r)
{
case NSAlertAlternateReturn:
return NO;
break;
case NSAlertDefaultReturn:
[self save];
break;
}
}
[tabView removeTabViewItem: tabViewItem];
return YES;
}
- (BOOL) isEdited
{
return edited;
}
- (NSString *) fileName
{
return fileName;
}
- (NSFont *) font
{
return [textView font];
}
- (void) setFont: (NSFont *) newFont
{
[textView setFont: newFont];
}
- (void) dealloc
{
[textView release];
[tabViewItem release];
[fileName release];
[super dealloc];
}
// delegate for TextView
- (void) textDidChange: (NSNotification*) notification;
{
edited = YES;
}
@end