Skip to content

Instantly share code, notes, and snippets.

@visigoth
Created November 25, 2010 20:55
Show Gist options
  • Save visigoth/715903 to your computer and use it in GitHub Desktop.
Save visigoth/715903 to your computer and use it in GitHub Desktop.
@interface HostView : NSView {
NSView *_docView;
CGFloat _scale;
}
@property (assign) CGFloat scale;
@end
@implementation HostView
@synthesize scale=_scale;
- (void)updateFrame {
NSRect frame = _docView.frame;
// When in landscape mode, flip around the width/height.
if ([_docView frameRotation] == 90.) {
CGFloat t = frame.size.width;
frame.size.width = frame.size.height;
frame.size.height = t;
}
frame.size.width = frame.size.width * _scale;
frame.size.height = frame.size.height * _scale;
self.frame = frame;
}
- (void)setScale:(CGFloat)f {
CGFloat s = f / _scale;
[docView scaleUnitSquareToSize:NSMakeSize(s, s)];
_scale = f;
[self updateFrame];
}
- (void)setOrientation:(BOOL)landscape {
CGFloat rotationDegrees = 0.;
// When asked for landscape orientation, rotate counter-clockwise 90 degrees.
if (landscape) {
rotationDegrees = 90.;
}
// Rotate the document view. This will generate a frame changed notification
// that comes back to this object. The updateFrame: selector will get run to
// change the document frame. This will eventually allow the scroll view in
// which this view is hosted to recalculate its scroll extents.
[_docView setFrameCenterRotation:rotationDegrees];
// Oddly, rotation adjusts the frame but not by enough to accommodate a scroll
// view correctly. Adjust the frame ourselves.
NSRect frame = _docView.frame;
frame.origin = NSZeroPoint;
if (rotationDegrees == 90.) {
frame.origin.x = frame.size.height;
}
_docView.frame = frame;
[self setNeedsDisplay:YES];
}
- (void)documentFrameChanged:(NSNotification *)n {
[self updateFrame];
}
- (void)initWithDocumentView:(NSView *)docView {
_scale = 1.0;
_docView = [docView retain];
[self addSubview:_docView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(documentFrameChanged:) name:NSViewFrameDidChangeNotification object:_docView];
[self updateFrame];
}
- (void)dealloc {
[_docView release];
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment