PDF Viewer question
PDF Viewer question
- Subject: PDF Viewer question
- From: Hasan Diwan <email@hidden>
- Date: Mon, 10 Mar 2003 09:01:12 -0800
I just wrote a PDF viewer after getting annoyed with Preview.app and
Acrobat's lack of speed. The NSView subclass is pasted below. If I drag
a PDF file into the view, the first page only displays, I'd like to
have a vertical scrollbar to display the rest of the pages. Any help
appreciated. Thanks!
#import <Cocoa/Cocoa.h>
@interface MyImageView : NSView
{
NSImage *_image;
NSColor *_bgColor;
NSImageScaling _scaling;
}
- (void)setImage:(NSImage*)image;
- (void)setImageScaling:(NSImageScaling)newScaling;
- (void)setBackgroundColor:(NSColor*)color;
- (NSImage*)image;
- (NSColor*)backgroundColor;
- (NSImageScaling)imageScaling;
@end
@implementation MyImageView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_bgColor = [[NSColor clearColor] retain];
_scaling = NSScaleProportionally;
}
[self registerForDraggedTypes:[NSArray
arrayWithObjects:NSPostScriptPboardType, NSPICTPboardType,
NSPDFPboardType, NSFileContentsPboardType, NSTIFFPboardType,
NSFilenamesPboardType, nil]];
return self;
}
- (void)dealloc
{
[_image release];
[_bgColor release];
[super dealloc];
}
- (void) setImage:(NSImage*)image
{
NSImage *tmp = [image retain];
[_image release];
_image = tmp;
[self setNeedsDisplay:YES];
}
- (NSImage*)image
{
return _image;
}
- (void)setImageScaling:(NSImageScaling)newScaling
{
_scaling = newScaling;
[self setNeedsDisplay:YES];
}
- (NSImageScaling)imageScaling
{
return _scaling;
}
- (void)setBackgroundColor:(NSColor*)color
{
NSColor *tmp = [color retain];
[_bgColor release];
_bgColor = tmp;
[self setNeedsDisplay:YES];
}
- (NSColor*)backgroundColor
{
return _bgColor;
}
- (void) drawRect:(NSRect)rects
{
NSRect bounds = [self bounds];
[_bgColor set];
[NSBezierPath fillRect:bounds];
if (_image) {
NSImage *copy = [_image copy];
NSSize size = [copy size];
float rx, ry, r;
NSPoint pt;
switch (_scaling) {
case NSScaleProportionally:
rx = bounds.size.width / size.width;
ry = bounds.size.height / size.height;
r = rx < ry ? rx : ry;
size.width *= r;
size.height *= r;
[copy setSize:size];
break;
case NSScaleToFit:
size = bounds.size;
[copy setSize:size];
break;
case NSScaleNone:
break;
default:
;
}
pt.x = (bounds.size.width - size.width) / 2;
pt.y = (bounds.size.height - size.height) / 2;
[copy compositeToPoint:pt operation:NSCompositeCopy];
[copy release];
}
}
- (unsigned int)draggingEntered:(id <NSDraggingInfo>)sender {
if ([[sender draggingPasteboard] availableTypeFromArray:[NSArray
arrayWithObject:NSFilenamesPboardType]]) {
return NSDragOperationCopy;
}
return NSDragOperationNone;
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
NSPasteboard *paste = [sender draggingPasteboard];
NSArray *types = [NSArray arrayWithObjects:NSPostScriptPboardType,
NSPICTPboardType, NSPDFPboardType, NSFileContentsPboardType,
NSTIFFPboardType, NSFilenamesPboardType, nil];
NSString *desiredType = [paste availableTypeFromArray:types];
NSImage *newImage = nil;
if ([desiredType isEqualToString:NSFilenamesPboardType])
{
NSArray *fileArray = [paste
propertyListForType:@"NSFilenamesPboardType"];
NSString *path = [fileArray objectAtIndex:0];
newImage = [[NSImage alloc] initWithContentsOfFile:path];
if (nil == newImage)
{
//complain since it failed (panel or something)
return NO;
}
}
else
{
NSData *carriedData = [paste dataForType:desiredType];
if (nil == carriedData)
{
//data was invalid so complain
return NO;
}
else
{
newImage = [[NSImage alloc] initWith
Data:carriedData];
if (nil == newImage)
{
return NO;
}
}
}
[self setImage:newImage];
[newImage release];
[self setNeedsDisplay:YES]; //redraw us with the new image
return YES;
}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender {
return YES;
}
@end
Hasan Diwan
OpenPGP KeyID: 0x7EE3855B
Fingerprint: 42F0 5758 C3EB BA1F ABD2 ED49 3390 CCF0 7EE3 855B
http://www.cs.rpi.edu/~diwanh/gpg.key
[demime 0.98b removed an attachment of type application/pgp-signature which had a name of PGP.sig]
_______________________________________________
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.