Custom view inside a PDFView
Custom view inside a PDFView
- Subject: Custom view inside a PDFView
- From: Hugues Pichereau <email@hidden>
- Date: Mon, 1 May 2006 19:25:54 +0200
- Resent-date: Mon, 1 May 2006 19:45:59 +0200
- Resent-from: Hugues Pichereau <email@hidden>
- Resent-message-id: <email@hidden>
- Resent-to: email@hidden
Hi,
I would like to insert custom views inside a PDFView. The custom view
should contain other views such as a NSTextView, and should resize
and move along with the PDF page (like the "text annotation" in
Preview).
This causes many issues (see below). Yes, I'm new to Cooca, but have
carefully read the Apple guides on View Prog. and Drawing, and spent
about 15 hours (don't laugh !) with this damn thing.
Here is the test code (adapted from the Apple's PDFLinker2 sample):
=========================================================
/* MyPDFView */
#import <Cocoa/Cocoa.h>
#import <Quartz/Quartz.h>
#import "MyWidget.h"
@interface MyPDFView : PDFView {
IBOutlet MyWidget *_widget;
NSTextView *_textView;
}
- (void) transformContextForPage: (PDFPage *) page;
@end
----------------------------------------------------------
#import "MyPDFView.h"
#import "MyWidget.h"
static NSRect RectPlusScale (NSRect aRect, float scale);
@implementation MyPDFView
- (void)setDocument:(PDFDocument *)document {
[super setDocument:document];
_widget = [[MyWidget alloc] initWithFrame:NSMakeRect(200,10,100,100)];
[self addSubview:_widget];
_textView = [[NSTextView alloc] initWithFrame:NSMakeRect(10, 10, 80,
80)];
[_widget addSubview:_textView];
[_widget setAutoresizesSubviews:YES];
[_widget setAutoresizingMask:NSViewHeightSizable];
[_widget setAutoresizingMask:NSViewWidthSizable];
[_textView setBackgroundColor:[NSColor greenColor]];
[[[_textView textStorage] mutableString] appendString:@"abc"];
[_textView setEditable:YES];
[_textView setAutoresizesSubviews:YES];
[_textView setAutoresizingMask:NSViewHeightSizable];
[_textView setAutoresizingMask:NSViewWidthSizable];
}
- (void) drawPage: (PDFPage *) pdfPage {
[super drawPage: pdfPage];
[self transformContextForPage: pdfPage];
NSRect test = NSMakeRect(200,10,100,100);
NSRect testTrans = [self convertRect:test fromPage:pdfPage];
[_widget setFrame: testTrans];
// [_widget setNeedsDisplay:YES] doesn't help
// [_widget display] causes a fatal loop
[_textView display]; // this one seems to be ok
}
- (void) transformContextForPage: (PDFPage *) page {
NSAffineTransform *transform;
NSRect boxRect;
boxRect = [page boundsForBox: [self displayBox]];
transform = [NSAffineTransform transform];
[transform translateXBy: -boxRect.origin.x yBy: -boxRect.origin.y];
[transform concat];
}
// etc...
=========================================================
/* MyWidget */
#import <Cocoa/Cocoa.h>
@interface MyWidget : NSView {
}
@end
----------------------------------------------------------
#import "MyWidget.h"
@implementation MyWidget
- (id)initWithFrame:(NSRect)frameRect {
if ((self = [super initWithFrame: frameRect]) != nil) {
}
return self;
}
- (void)drawRect:(NSRect)aRect {
[[NSColor colorWithDeviceRed:1 green:0.9 blue:0.5 alpha:0.8] set];
[NSBezierPath fillRect: aRect];
}
@end
=========================================================
Problems:
1)
The drawRect of MyWidget is called only when MyWidget is clipping at
the PDFView borders. Each time a drawPage in MyPDFView occurs,
there's no drawRect of MyWidget. So the widget is not repainted
(hatched) when scrolling or zooming the page.
Adding a setNeedsDisplay did not triggered more drawRect. There's
probably a better place to call it from, but I don't know where...
Forcing a [_widget display] causes the app to freeze in some kind of
nasty loop.
2)
When zooming the page, the inner _textView is not resized (its
content is just clipped inside the _widget). I quickly tried to set
manually in drawPage its bounds and frame (as for the _widget), but
it did not seem to work.
3)
The _textView repaints over the scrollbars of the PDFView. Adding in
MyPDFView's drawPage a [self setNeedsDisplay] (probably a silly idea)
did not help. I did not find the exact inner rect excluding the
scrollbars, to call a cleaner [_textView displayRect:].
I'm probably missing a lot of things, or is the PDFView really not
made for adding custom views ?
Thanks,
Hugo
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden