Skip to content

Instantly share code, notes, and snippets.

@visigoth
Created November 25, 2010 20:55
Show Gist options
  • Select an option

  • Save visigoth/715903 to your computer and use it in GitHub Desktop.

Select an option

Save visigoth/715903 to your computer and use it in GitHub Desktop.

Revisions

  1. visigoth revised this gist Jul 9, 2011. 2 changed files with 83 additions and 49 deletions.
    83 changes: 83 additions & 0 deletions gistfile1.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,83 @@
    @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
    49 changes: 0 additions & 49 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -1,49 +0,0 @@
    @interface HostView : NSView {
    NSView *_docView;
    CGFloat _scale;
    }

    @property (assign) CGFloat scale;

    @end



    @implementation HostView

    @synthesize scale=_scale;

    - (void)updateFrame {
    NSRect frame = _docView.frame;
    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)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
  2. visigoth revised this gist Nov 25, 2010. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -21,8 +21,9 @@ - (void)updateFrame {
    }

    - (void)setScale:(CGFloat)f {
    CGFloat s = f / _scale;
    [docView scaleUnitSquareToSize:NSMakeSize(s, s)];
    _scale = f;
    [docView scaleUnitSquareToSize:NSMakeSize(_scale, _scale)];
    [self updateFrame];
    }

  3. visigoth renamed this gist Nov 25, 2010. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. visigoth created this gist Nov 25, 2010.
    48 changes: 48 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    @interface HostView : NSView {
    NSView *_docView;
    CGFloat _scale;
    }

    @property (assign) CGFloat scale;

    @end



    @implementation HostView

    @synthesize scale=_scale;

    - (void)updateFrame {
    NSRect frame = _docView.frame;
    frame.size.width = frame.size.width * _scale;
    frame.size.height = frame.size.height * _scale;
    self.frame = frame;
    }

    - (void)setScale:(CGFloat)f {
    _scale = f;
    [docView scaleUnitSquareToSize:NSMakeSize(_scale, _scale)];
    [self updateFrame];
    }

    - (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