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
|
/*
Document.m
Copyright (c) 1995-1996, NeXT Software, Inc.
All rights reserved.
Author: Ali Ozer
You may freely copy, distribute and reuse the code in this example.
NeXT disclaims any warranty of any kind, expressed or implied,
as to its fitness for any particular use.
Document object for Edit...
*/
#import <AppKit/AppKit.h>
#import <math.h>
#import <stdio.h> // for NULL
#import "Document.h"
#import "MultiplePageView.h"
#import "TextFinder.h"
@implementation Document
- (void)setupInitialTextViewSharedState
{
NSText *textView = [self firstTextView];
[textView setUsesFontPanel:YES];
[textView setDelegate:self];
[textView setRichText: YES];
// [self setRichText:[[Preferences objectForKey:RichText] boolValue]];
// [self setHyphenationFactor:0.0];
}
- (id)init
{
static NSPoint cascadePoint = {0.0, 0.0};
NSZone *zone = [self zone];
self = [super init];
// This ensures the first view gets set up correctly
[self setupInitialTextViewSharedState];
return self;
}
- (id)initWithPath:(NSString *)filename encoding:(int)encoding
uniqueZone:(BOOL)flag
{
if (!(self = [self init])) {
if (flag) NSRecycleZone([self zone]);
return nil;
}
uniqueZone = flag; /* So if something goes wrong we can recycle the zone correctly in dealloc */
if (filename && ![self loadFromPath:filename encoding:encoding]) {
[self release];
return nil;
}
if (filename) {
[Document setLastOpenSavePanelDirectory:[filename stringByDeletingLastPathComponent]];
}
[[self firstTextView] setSelectedRange:NSMakeRange(0, 0)];
[self setDocumentName:filename];
return self;
}
+ (BOOL)openUntitled
{
NSZone *zone = NSCreateZone(NSPageSize(), NSPageSize(), YES);
Document *document = [[self allocWithZone:zone] initWithPath:nil
encoding:UnknownStringEncoding
uniqueZone:YES];
if (document)
{
[document setPotentialSaveDirectory:[Document openSavePanelDirectory]];
[document setDocumentName:nil];
[[document window] makeKeyAndOrderFront:nil];
return YES;
}
else
return NO;
}
/* Return the document in the specified window.
*/
+ (Document *)documentForWindow:(NSWindow *)window {
id delegate = [window delegate];
return (delegate && [delegate isKindOfClass:[Document class]]) ? delegate : nil;
}
+ (Document *)documentForPath:(NSString *)path {
return nil;
}
+ (BOOL)openDocumentWithPath:(NSString *)filename encoding:(int)encoding {
Document *document = [self documentForPath:filename];
if (!document) {
NSZone *zone = NSCreateZone(NSPageSize(), NSPageSize(), YES);
document = [[self allocWithZone:zone] initWithPath:filename encoding:encoding uniqueZone:YES];
}
}
/* Clear the delegates of the text views and window, then release all resources and go away...
*/
- (void)dealloc
{
[[self firstTextView] setDelegate:nil];
[[self window] setDelegate:nil];
[documentName release];
if (uniqueZone) {
NSRecycleZone([self zone]);
}
[super dealloc];
}
- (NSText *)firstTextView
{
static textView;
if(!textView)
textView = [NSText new];
return textView;
}
- (NSSize)viewSize
{
return [scrollView contentSize];
}
- (void)setViewSize:(NSSize)size {
NSWindow *window = [scrollView window];
NSRect origWindowFrame = [window frame];
NSSize scrollViewSize = [NSScrollView frameSizeForContentSize:size hasHorizontalScroller:[scrollView hasHorizontalScroller] hasVerticalScroller:[scrollView hasVerticalScroller] borderType:[scrollView borderType]];
[window setContentSize:scrollViewSize];
[window setFrameTopLeftPoint:NSMakePoint(origWindowFrame.origin.x, NSMaxY(origWindowFrame))];
}
/* This method causes the text to be laid out in the foreground (approximately) up to the indicated character index.
*/
- (void)doForegroundLayoutToCharacterIndex:(unsigned)loc
{}
+ (NSString *)cleanedUpPath:(NSString *)filename {
NSString *resolvedSymlinks = [filename stringByResolvingSymlinksInPath];
if ([resolvedSymlinks length] > 0) {
NSString *standardized = [resolvedSymlinks stringByStandardizingPath];
return [standardized length] ? standardized : resolvedSymlinks;
}
return filename;
}
- (void)setDocumentName:(NSString *)filename {
[documentName autorelease];
if (filename) {
documentName = [[filename stringByResolvingSymlinksInPath] copyWithZone:[self zone]];
[[self window] setTitleWithRepresentedFilename:documentName];
if (uniqueZone) NSSetZoneName([self zone], documentName);
} else {
NSString *untitled = NSLocalizedString(@"UNTITLED", @"Name of new, untitled document");
if ([self isRichText]) untitled = [untitled stringByAppendingPathExtension:@"rtf"];
if (potentialSaveDirectory) {
[[self window] setTitleWithRepresentedFilename:[potentialSaveDirectory stringByAppendingPathComponent:untitled]];
} else {
[[self window] setTitle:untitled];
}
if (uniqueZone) NSSetZoneName([self zone], untitled);
documentName = nil;
}
}
- (NSString *)documentName
{
return documentName;
}
- (BOOL)isRichText
{
return isRichText;
}
- (void)setPotentialSaveDirectory:(NSString *)nm
{}
- (NSString *)potentialSaveDirectory
{}
- (void)setDocumentEdited:(BOOL)flag
{
if (flag != isDocumentEdited) {
isDocumentEdited = flag;
[[self window] setDocumentEdited:isDocumentEdited];
}
}
- (BOOL)isDocumentEdited
{
return isDocumentEdited;
}
- (NSWindow *)window
{
return [[self firstTextView] window];
}
- (void)setPrintInfo:(NSPrintInfo *)anObject
{}
- (NSPrintInfo *)printInfo
{
return printInfo;
}
/* Multiple page related code */
- (unsigned)numberOfPages {
if (hasMultiplePages) {
return [[scrollView documentView] numberOfPages];
} else {
return 1;
}
}
- (BOOL)hasMultiplePages {
return hasMultiplePages;
}
/* Outlet methods */
- (void)setScrollView:(id)anObject
{
scrollView = anObject;
[scrollView setHasVerticalScroller:YES];
[scrollView setHasHorizontalScroller:NO];
[[scrollView contentView] setAutoresizesSubviews:YES];
[self fixUpScrollViewBackgroundColor:nil];
}
static NSPopUpButton *encodingPopupButton = nil;
static NSView *encodingAccessory = nil;
- (void)close:(id)sender
{
[[self window] close];
}
/* Not correct! */
- (void)saveTo:(id)sender
{
[self saveAs:sender];
}
- (void)saveAs:(id)sender
{
(void)[self saveDocument:YES];
}
- (void)save:(id)sender
{
(void)[self saveDocument:NO];
}
- (BOOL)saveDocument:(BOOL)showSavePanel
{
NSLog(@"Save called!");
[[self firstTextView] writeRTFDToFile: @"test.rtf" atomically: NO];
return YES;
}
/* Window delegation messages */
- (BOOL)windowShouldClose:(id)sender
{
//return [self canCloseDocument];
return YES;
}
- (void)windowWillClose:(NSNotification *)notification
{
NSWindow *window = [self window];
[window setDelegate:nil];
[self release];
}
/* Text view delegation messages */
- (void)textDidChange:(NSNotification *)textObject
{
//[self saveDocument: NO];
if (!isDocumentEdited) {
[self setDocumentEdited:YES];
}
}
@end
|