TextView problem
TextView problem
- Subject: TextView problem
- From: Stephane Curzi <email@hidden>
- Date: Thu, 05 Jun 2003 16:07:28 -0400
Hi,
I have this code, almost everything works ok. I cant load text, write
text but I can't save or print, can someone help me here ?
//
// MyDocument.m
#import "MyDocument.h"
#import "TextFinder.h"
const float LargeNumberForText = 1.0e7;
@implementation MyDocument
- (id)init
{
self = [super init];
if (self) {
// Add your subclass-specific initialization here.
}
return self;
}
- (NSString *)windowNibName
{
return @"MyDocument";
}
- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
NSWindow *documentWindow;
NSScrollView *scrollView;
NSRect scrollViewRect;
NSLayoutManager *layoutManager;
NSTextStorage *textStorage;
NSTextContainer *textContainer;
NSTextView *textView;
NSRect frame;
NSRect textFrame;
NSRect textRect;
[super windowControllerDidLoadNib:aController];
documentWindow=[[[self windowControllers] lastObject] window];
scrollViewRect=[NSWindow contentRectForFrameRect:[documentWindow
frame]
styleMask:[documentWindow
styleMask]];
scrollView = [[NSScrollView alloc] initWithFrame:scrollViewRect];
[scrollView setBorderType:NSNoBorder];
[scrollView setHasVerticalScroller:YES];
[scrollView setHasHorizontalScroller:YES];
[scrollView setAutoresizingMask:(NSViewWidthSizable |
NSViewHeightSizable)];
[[scrollView contentView] setAutoresizesSubviews:YES];
layoutManager = [[NSLayoutManager allocWithZone:[self zone]] init];
textStorage = [[NSTextStorage alloc] init];
[textStorage addLayoutManager:layoutManager];
textContainer = [[NSTextContainer allocWithZone:[self zone]]
initWithContainerSize:NSMakeSize(LargeNumberForText,
LargeNumberForText)];
[textContainer setWidthTracksTextView:NO];
[textContainer setHeightTracksTextView:NO];
[layoutManager addTextContainer:textContainer];
textView = [[NSTextView allocWithZone:[self zone]]
initWithFrame:textFrame textContainer:textContainer];
[textView setMinSize:textFrame.size];
[textView setMaxSize:NSMakeSize(LargeNumberForText,
LargeNumberForText)];
[textView setHorizontallyResizable:YES];
[textView setVerticallyResizable:YES];
[textView setAutoresizingMask:NSViewNotSizable];
[textView setSelectable:YES];
[textView setEditable:YES];
[textView setAllowsUndo:YES];
[textView setRichText:NO];
[textView setImportsGraphics:NO];
[textView setUsesFontPanel:NO];
[textView setUsesRuler:NO];
[scrollView setDocumentView:textView];
[documentWindow setContentView:scrollView];
[scrollView release];
[textContainer release];
[textView release];
[[textView window] makeFirstResponder:textView];
[self setupToolbar];
if (dataFromFile){
NSString *text = [[NSString alloc]initWith
Data:dataFromFile
encoding:NSMacOSRomanStringEncoding];
[textView setString:text];
[text release];
}
NSFont *theFont;
float defaultFontSize;
defaultFontSize=[[NSUserDefaults standardUserDefaults]
floatForKey:@"FontSize"];
theFont=[NSFont fontWithName:[[NSUserDefaults standardUserDefaults]
stringForKey:@"FontFace"]
size:defaultFontSize];
if (!theFont)
theFont=[NSFont userFixedPitchFontOfSize:defaultFontSize];
[textView setFont:theFont];
[textView setMenu:nil];
}
- (NSData *)dataRepresentationOfType:(NSString *)aType
{
NSString *text = [textView string];
return [text dataUsingEncoding:NSUTF8StringEncoding];
}
-(BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
if (dataFromFile){
NSString *text = [[NSString alloc]initWith
Data:dataFromFile
encoding:NSUTF8StringEncoding];
[textView setString:text];
[text release];
} else {
dataFromFile = [data retain];
}
return YES;
}
-(void)dealloc
{
[dataFromFile release];
[super dealloc];
}
- (void)printDocument:(id)sender {
NSPrintInfo * printInfo = [self printInfo];
NSPrintOperation * printOperation;
printOperation = [NSPrintOperation
printOperationWithView:textStorage
printInfo:printInfo];
[printOperation runOperationModalForWindow: documentWindow
delegate: self
didRunSelector: NULL
contextInfo: NULL];
}
@end
--
Stiphane Curzi
email@hidden
http://www.projetsurbain.com/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.