Skip to content

Commit

Permalink
Zoom in/Out
Browse files Browse the repository at this point in the history
  • Loading branch information
pppoe committed Aug 10, 2011
1 parent edd34fa commit 47e88c3
Show file tree
Hide file tree
Showing 6 changed files with 657 additions and 63 deletions.
13 changes: 7 additions & 6 deletions GetFrame.xcodeproj/project.pbxproj
Expand Up @@ -12,7 +12,7 @@
4654F92713F10E2700BE97F7 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 4654F92613F10E2700BE97F7 /* main.m */; }; 4654F92713F10E2700BE97F7 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 4654F92613F10E2700BE97F7 /* main.m */; };
4654F92A13F10E2700BE97F7 /* Credits.rtf in Resources */ = {isa = PBXBuildFile; fileRef = 4654F92813F10E2700BE97F7 /* Credits.rtf */; }; 4654F92A13F10E2700BE97F7 /* Credits.rtf in Resources */ = {isa = PBXBuildFile; fileRef = 4654F92813F10E2700BE97F7 /* Credits.rtf */; };
4654F92D13F10E2700BE97F7 /* GetFrameAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 4654F92C13F10E2700BE97F7 /* GetFrameAppDelegate.m */; }; 4654F92D13F10E2700BE97F7 /* GetFrameAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 4654F92C13F10E2700BE97F7 /* GetFrameAppDelegate.m */; };
4654F93013F10E2800BE97F7 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4654F92E13F10E2800BE97F7 /* MainMenu.xib */; }; 4654F93013F10E2800BE97F7 /* Main.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4654F92E13F10E2800BE97F7 /* Main.xib */; };
4654F93813F119D700BE97F7 /* GetFrameWinController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4654F93713F119D700BE97F7 /* GetFrameWinController.m */; }; 4654F93813F119D700BE97F7 /* GetFrameWinController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4654F93713F119D700BE97F7 /* GetFrameWinController.m */; };
4654F93A13F11A0300BE97F7 /* GetFrameWinController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4654F93913F11A0300BE97F7 /* GetFrameWinController.xib */; }; 4654F93A13F11A0300BE97F7 /* GetFrameWinController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4654F93913F11A0300BE97F7 /* GetFrameWinController.xib */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
Expand All @@ -30,7 +30,7 @@
4654F92913F10E2700BE97F7 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.rtf; name = en; path = en.lproj/Credits.rtf; sourceTree = "<group>"; }; 4654F92913F10E2700BE97F7 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.rtf; name = en; path = en.lproj/Credits.rtf; sourceTree = "<group>"; };
4654F92B13F10E2700BE97F7 /* GetFrameAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GetFrameAppDelegate.h; sourceTree = "<group>"; }; 4654F92B13F10E2700BE97F7 /* GetFrameAppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GetFrameAppDelegate.h; sourceTree = "<group>"; };
4654F92C13F10E2700BE97F7 /* GetFrameAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GetFrameAppDelegate.m; sourceTree = "<group>"; }; 4654F92C13F10E2700BE97F7 /* GetFrameAppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GetFrameAppDelegate.m; sourceTree = "<group>"; };
4654F92F13F10E2800BE97F7 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/MainMenu.xib; sourceTree = "<group>"; }; 4654F92F13F10E2800BE97F7 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/Main.xib; sourceTree = "<group>"; };
4654F93613F119D700BE97F7 /* GetFrameWinController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GetFrameWinController.h; sourceTree = "<group>"; }; 4654F93613F119D700BE97F7 /* GetFrameWinController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GetFrameWinController.h; sourceTree = "<group>"; };
4654F93713F119D700BE97F7 /* GetFrameWinController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GetFrameWinController.m; sourceTree = "<group>"; }; 4654F93713F119D700BE97F7 /* GetFrameWinController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GetFrameWinController.m; sourceTree = "<group>"; };
4654F93913F11A0300BE97F7 /* GetFrameWinController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = GetFrameWinController.xib; sourceTree = "<group>"; }; 4654F93913F11A0300BE97F7 /* GetFrameWinController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = GetFrameWinController.xib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -89,7 +89,7 @@
children = ( children = (
4654F92B13F10E2700BE97F7 /* GetFrameAppDelegate.h */, 4654F92B13F10E2700BE97F7 /* GetFrameAppDelegate.h */,
4654F92C13F10E2700BE97F7 /* GetFrameAppDelegate.m */, 4654F92C13F10E2700BE97F7 /* GetFrameAppDelegate.m */,
4654F92E13F10E2800BE97F7 /* MainMenu.xib */, 4654F92E13F10E2800BE97F7 /* Main.xib */,
4654F92013F10E2700BE97F7 /* Supporting Files */, 4654F92013F10E2700BE97F7 /* Supporting Files */,
4654F93613F119D700BE97F7 /* GetFrameWinController.h */, 4654F93613F119D700BE97F7 /* GetFrameWinController.h */,
4654F93713F119D700BE97F7 /* GetFrameWinController.m */, 4654F93713F119D700BE97F7 /* GetFrameWinController.m */,
Expand Down Expand Up @@ -162,7 +162,7 @@
files = ( files = (
4654F92413F10E2700BE97F7 /* InfoPlist.strings in Resources */, 4654F92413F10E2700BE97F7 /* InfoPlist.strings in Resources */,
4654F92A13F10E2700BE97F7 /* Credits.rtf in Resources */, 4654F92A13F10E2700BE97F7 /* Credits.rtf in Resources */,
4654F93013F10E2800BE97F7 /* MainMenu.xib in Resources */, 4654F93013F10E2800BE97F7 /* Main.xib in Resources */,
4654F93A13F11A0300BE97F7 /* GetFrameWinController.xib in Resources */, 4654F93A13F11A0300BE97F7 /* GetFrameWinController.xib in Resources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -199,12 +199,12 @@
name = Credits.rtf; name = Credits.rtf;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
4654F92E13F10E2800BE97F7 /* MainMenu.xib */ = { 4654F92E13F10E2800BE97F7 /* Main.xib */ = {
isa = PBXVariantGroup; isa = PBXVariantGroup;
children = ( children = (
4654F92F13F10E2800BE97F7 /* en */, 4654F92F13F10E2800BE97F7 /* en */,
); );
name = MainMenu.xib; name = Main.xib;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
/* End PBXVariantGroup section */ /* End PBXVariantGroup section */
Expand Down Expand Up @@ -291,6 +291,7 @@
4654F93513F10E2800BE97F7 /* Release */, 4654F93513F10E2800BE97F7 /* Release */,
); );
defaultConfigurationIsVisible = 0; defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
}; };
/* End XCConfigurationList section */ /* End XCConfigurationList section */
}; };
Expand Down
2 changes: 1 addition & 1 deletion GetFrame/GetFrame-Info.plist
Expand Up @@ -25,7 +25,7 @@
<key>LSMinimumSystemVersion</key> <key>LSMinimumSystemVersion</key>
<string>${MACOSX_DEPLOYMENT_TARGET}</string> <string>${MACOSX_DEPLOYMENT_TARGET}</string>
<key>NSMainNibFile</key> <key>NSMainNibFile</key>
<string>MainMenu</string> <string>Main</string>
<key>NSPrincipalClass</key> <key>NSPrincipalClass</key>
<string>NSApplication</string> <string>NSApplication</string>
</dict> </dict>
Expand Down
11 changes: 11 additions & 0 deletions GetFrame/GetFrameWinController.h
Expand Up @@ -9,10 +9,21 @@
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>


@interface GetFrameWinController : NSWindowController { @interface GetFrameWinController : NSWindowController {

@private @private
IBOutlet NSImageView *_imageView; IBOutlet NSImageView *_imageView;
IBOutlet NSScrollView *_scrollView;


//< For Zooming
NSSize _orgSize; //< Original Image Size
float _zoomFactor;
} }


- (IBAction)openImage:(id)sender; - (IBAction)openImage:(id)sender;


- (IBAction)modeGetFrame:(id)sender;
- (IBAction)zoomIn:(id)sender;
- (IBAction)zoomOut:(id)sender;

@end @end
101 changes: 101 additions & 0 deletions GetFrame/GetFrameWinController.m
Expand Up @@ -8,6 +8,32 @@


#import "GetFrameWinController.h" #import "GetFrameWinController.h"


/////////////////////////////////////////////////////////////////////
//< Constants
/////////////////////////////////////////////////////////////////////
#define kMinWinWidth (400)
#define kMinWinHeight (400)

#define kMaxWinWidth (800)
#define kMaxWinHeight (800)

#define kImagePadding (5)

#define kMaxImageWidth (5000)
#define kMaxImageHeight (5000)

#define kMinImageWidth (16)
#define kMinImageHeight (16)

#define kMinZoomFactor (1.0f)
#define kMaxZoomFactor (8.0f)

@interface GetFrameWinController (Private)

- (void)adjustFrameForNewImage;
- (void)zoomView:(NSView *)view withFactor:(float)factor;

@end


@implementation GetFrameWinController @implementation GetFrameWinController


Expand All @@ -29,6 +55,14 @@ - (void)dealloc
- (void)windowDidLoad - (void)windowDidLoad
{ {
[super windowDidLoad]; [super windowDidLoad];

[[self window] setMinSize:NSMakeSize(kMinWinWidth, kMinWinHeight)];
[[self window] setMaxSize:NSMakeSize(kMaxWinWidth, kMaxWinHeight)];

_orgSize = [_imageView frame].size;
_zoomFactor = 1.0f;

[_scrollView setDocumentView:_imageView];
} }


- (IBAction)openImage:(id)sender { - (IBAction)openImage:(id)sender {
Expand All @@ -46,8 +80,75 @@ - (IBAction)openImage:(id)sender {
{ {
[_imageView setImage:image]; [_imageView setImage:image];
[image release]; [image release];

[self adjustFrameForNewImage];
} }
} }
} }


- (IBAction)modeGetFrame:(id)sender {

}

- (IBAction)zoomIn:(id)sender {

_zoomFactor = MIN(kMaxZoomFactor, 2 * _zoomFactor);

[self zoomView:_imageView withFactor:_zoomFactor];
}

- (IBAction)zoomOut:(id)sender {

_zoomFactor = MAX(kMinZoomFactor, _zoomFactor / 2.0f);

[self zoomView:_imageView withFactor:_zoomFactor];
}

@end

@implementation GetFrameWinController (Private)

- (void)zoomView:(NSView *)view withFactor:(float)factor {
NSSize s = [view frame].size;
NSPoint org = [view frame].origin;
NSPoint ctrPt = NSMakePoint(org.x + s.width/2.0f, org.y + s.height/2.0f);
NSSize newSize = NSMakeSize(_orgSize.width * factor, _orgSize.height * factor);
[[view animator] setFrame:NSMakeRect(ctrPt.x - newSize.width/2.0f,
ctrPt.y - newSize.height/2.0f,
newSize.width, newSize.height)];
}

- (void)adjustFrameForNewImage
{

// NSSize size = [[_imageView image] size];
//
// //< Keep the Left and Bottom Padding
// float leftPadding = _imageView.frame.origin.x;
// float bottomPadding = _imageView.frame.origin.y;
//
// //< Keep the Center Point
// NSSize winSize = NSMakeSize(MIN(MAX((size.width + 2 * kImagePadding + 2 * leftPadding), kMinWinWidth), kMaxWinWidth),
// MIN(MAX((size.height + 2 * kImagePadding + 2 * bottomPadding), kMinWinHeight), kMaxWinHeight));
// NSRect oldWinFrame = [[self window] frame];
// NSPoint ctrPoint = NSMakePoint(oldWinFrame.origin.x + oldWinFrame.size.width/2.0f,
// oldWinFrame.origin.y + oldWinFrame.size.height/2.0f);
//
// NSRect winFrame = NSMakeRect(ctrPoint.x - winSize.width/2.0f,
// ctrPoint.y - winSize.height/2.0f,
// winSize.width, winSize.height);
// [[self window] setFrame:winFrame
// display:NO
// animate:YES];
//
// NSRect rect = NSMakeRect((winSize.width - (size.width + 2*kImagePadding))/2.0f,
// (winSize.height - (size.height + 2*kImagePadding))/2.0f,
// (size.width + 2*kImagePadding),
// (size.height + 2*kImagePadding));
// [_imageView setFrame:rect];
//
// [[self window] setMinSize:NSMakeSize(winFrame.size.width, winFrame.size.height)];

}

@end @end

0 comments on commit 47e88c3

Please sign in to comment.