Re: PDF Viewer question
Re: PDF Viewer question
- Subject: Re: PDF Viewer question
- From: Hasan Diwan <email@hidden>
- Date: Mon, 10 Mar 2003 10:12:24 -0800
Followup question:
How do I get the image from [NSPDFRep currentPage]?
>
#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];
if ([[path extension] isEqualToString:@"pdf"]) {
NSPDFImageRep *pdfFile= [NSPDFImageRep
imageRepWithContentsOfFile:path];
int n;
for (n = 0; n != [pdfFile pageCount]; n++) {
// need to somehow create image with the data from the nth page
[scroller addSubView:[NSImageView setImage:[[[NSImage alloc]
initWith
Data:[pdfRep objectAtIndex:n]] autorelease]]];
[self setNeedsRedisplay:YES];
}
}
>
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] initWithData: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.
>
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.