Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 0878bbef7fa626c3bb6f753edf8555cee39fb310 0 parents
@davedelong authored
6 .gitignore
@@ -0,0 +1,6 @@
+build
+**/*.mode1v3
+**/*.perspectivev3
+**/*.pbxuser
+**/*.xcworkspace
+**/xcuserdata
28 CHLayout.h
@@ -0,0 +1,28 @@
+//
+// CHLayout.h
+// CHLayoutManager
+/**
+ Copyright (c) 2010 Dave DeLong
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ **/
+
+#import "NSView+CHLayout.h"
+#import "CHLayoutManager.h"
+#import "CHLayoutConstraint.h"
63 CHLayoutConstraint.h
@@ -0,0 +1,63 @@
+//
+// CHLayoutConstraint.h
+// CHLayoutManager
+/**
+ Copyright (c) 2010 Dave DeLong
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ **/
+
+#import <Cocoa/Cocoa.h>
+
+typedef enum {
+ CHLayoutConstraintAttributeMinY = 1, //the left edge
+ CHLayoutConstraintAttributeMaxY = 2, //the right edge
+ CHLayoutConstraintAttributeMinX = 3, //the bottom edge
+ CHLayoutConstraintAttributeMaxX = 4, //the top edge
+ CHLayoutConstraintAttributeWidth = 5, //the width
+ CHLayoutConstraintAttributeHeight = 6, //the height
+ CHLayoutConstraintAttributeMidY = 7,
+ CHLayoutConstraintAttributeMidX = 8
+} CHLayoutConstraintAttribute;
+
+@interface CHLayoutConstraint : NSObject {
+ CGFloat offset;
+ CGFloat scale;
+ CHLayoutConstraintAttribute attribute;
+
+ NSString * sourceName;
+ CHLayoutConstraintAttribute sourceAttribute;
+}
+
+@property (readonly) CGFloat offset;
+@property (readonly) CGFloat scale;
+@property (readonly) CHLayoutConstraintAttribute attribute;
+@property (readonly) CHLayoutConstraintAttribute sourceAttribute;
+@property (readonly) NSString * sourceName;
+
++ (id)constraintWithAttribute:(CHLayoutConstraintAttribute)attr relativeTo:(NSString *)srcLayer attribute:(CHLayoutConstraintAttribute)srcAttr;
++ (id)constraintWithAttribute:(CHLayoutConstraintAttribute)attr relativeTo:(NSString *)srcLayer attribute:(CHLayoutConstraintAttribute)srcAttr offset:(CGFloat)offset;
++ (id)constraintWithAttribute:(CHLayoutConstraintAttribute)attr relativeTo:(NSString *)srcLayer attribute:(CHLayoutConstraintAttribute)srcAttr scale:(CGFloat)scale offset:(CGFloat)offset;
+
+- (id)initWithAttribute:(CHLayoutConstraintAttribute)attr relativeTo:(NSString *)srcLayer attribute:(CHLayoutConstraintAttribute)srcAttr scale:(CGFloat)scale offset:(CGFloat)offset;
+
+- (CGFloat) transformValue:(CGFloat)original;
+- (void) applyToTargetView:(NSView *)target;
+
+@end
78 CHLayoutConstraint.m
@@ -0,0 +1,78 @@
+//
+// CHLayoutConstraint.m
+// CHLayoutManager
+/**
+ Copyright (c) 2010 Dave DeLong
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ **/
+
+#import "CHLayout.h"
+
+
+@implementation CHLayoutConstraint
+@synthesize offset, scale, attribute, sourceAttribute, sourceName;
+
+#pragma mark Basic Initializers
+
++ (id)constraintWithAttribute:(CHLayoutConstraintAttribute)attr relativeTo:(NSString *)srcLayer attribute:(CHLayoutConstraintAttribute)srcAttr {
+ return [self constraintWithAttribute:attr relativeTo:srcLayer attribute:srcAttr scale:1.0 offset:0.0];
+}
+
++ (id)constraintWithAttribute:(CHLayoutConstraintAttribute)attr relativeTo:(NSString *)srcLayer attribute:(CHLayoutConstraintAttribute)srcAttr offset:(CGFloat)offset {
+ return [self constraintWithAttribute:attr relativeTo:srcLayer attribute:srcAttr scale:1.0 offset:offset];
+}
+
++ (id)constraintWithAttribute:(CHLayoutConstraintAttribute)attr relativeTo:(NSString *)srcLayer attribute:(CHLayoutConstraintAttribute)srcAttr scale:(CGFloat)scale offset:(CGFloat)offset {
+ return [[[self alloc] initWithAttribute:attr relativeTo:srcLayer attribute:srcAttr scale:scale offset:offset] autorelease];
+}
+
+- (id)initWithAttribute:(CHLayoutConstraintAttribute)attr relativeTo:(NSString *)srcLayer attribute:(CHLayoutConstraintAttribute)srcAttr scale:(CGFloat)aScale offset:(CGFloat)anOffset {
+ if (self = [super init]) {
+ offset = anOffset;
+ scale = aScale;
+ attribute = attr;
+ sourceAttribute = srcAttr;
+ sourceName = [srcLayer copy];
+ }
+ return self;
+}
+
+- (void) dealloc {
+ [sourceName release];
+ [super dealloc];
+}
+
+- (CGFloat) transformValue:(CGFloat)original {
+ return (original * scale) + offset;
+}
+
+- (void) applyToTargetView:(NSView *)target {
+ NSView * source = [target relativeViewForName:[self sourceName]];
+ if (source == target) { return; }
+ if (source == nil) { return; }
+ if ([self sourceAttribute] == 0) { return; }
+
+ CGFloat sourceValue = [source valueForLayoutAttribute:[self sourceAttribute]];
+ CGFloat targetValue = [self transformValue:sourceValue];
+
+ [target setValue:targetValue forLayoutAttribute:[self attribute]];
+}
+
+@end
32 CHLayoutManager-Info.plist
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>CFBundleDevelopmentRegion</key>
+ <string>English</string>
+ <key>CFBundleExecutable</key>
+ <string>${EXECUTABLE_NAME}</string>
+ <key>CFBundleIconFile</key>
+ <string></string>
+ <key>CFBundleIdentifier</key>
+ <string>com.yourcompany.${PRODUCT_NAME:rfc1034identifier}</string>
+ <key>CFBundleInfoDictionaryVersion</key>
+ <string>6.0</string>
+ <key>CFBundleName</key>
+ <string>${PRODUCT_NAME}</string>
+ <key>CFBundlePackageType</key>
+ <string>APPL</string>
+ <key>CFBundleShortVersionString</key>
+ <string>1.0</string>
+ <key>CFBundleSignature</key>
+ <string>????</string>
+ <key>CFBundleVersion</key>
+ <string>1</string>
+ <key>LSMinimumSystemVersion</key>
+ <string>${MACOSX_DEPLOYMENT_TARGET}</string>
+ <key>NSMainNibFile</key>
+ <string>MainMenu</string>
+ <key>NSPrincipalClass</key>
+ <string>NSApplication</string>
+</dict>
+</plist>
42 CHLayoutManager.h
@@ -0,0 +1,42 @@
+//
+// CHLayoutManager.h
+// CHLayoutManager
+/**
+ Copyright (c) 2010 Dave DeLong
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ **/
+
+#import <Cocoa/Cocoa.h>
+
+@class CHLayoutConstraint;
+
+@interface CHLayoutManager : NSObject {
+ BOOL hasRegistered;
+ BOOL isProcessingChanges;
+
+ NSMutableArray * viewsToProcess;
+ NSMutableSet * processedViews;
+}
+
++ (id) sharedLayoutManager;
+
+- (void) beginProcessingView:(NSView *)aView;
+
+@end
156 CHLayoutManager.m
@@ -0,0 +1,156 @@
+//
+// CHLayoutManager.m
+// CHLayoutManager
+/**
+ Copyright (c) 2010 Dave DeLong
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ **/
+
+#import "CHLayoutManager.h"
+#import "CHLayoutConstraint.h"
+#import "NSView+CHLayout.h"
+#import <objc/runtime.h>
+
+static CHLayoutManager * _sharedLayoutManager = nil;
+
+__attribute__((constructor))
+static void construct_layoutManagerSingleton() {
+ NSAutoreleasePool * p = [[NSAutoreleasePool alloc] init];
+ _sharedLayoutManager = [[CHLayoutManager alloc] init];
+ [p release];
+}
+
+__attribute__((destructor))
+static void destroy_layoutManagerSingleton() {
+ //since this happens at some point during teardown, I'm not sure there'll be an autorelease pool in place
+ //but just in case.... make one anyway
+
+ NSAutoreleasePool * p = [[NSAutoreleasePool alloc] init];
+ [_sharedLayoutManager release], _sharedLayoutManager = nil;
+ [p release];
+}
+
+@implementation CHLayoutManager
+
++ (id) sharedLayoutManager {
+ return _sharedLayoutManager;
+}
+
++ (id) allocWithZone:(NSZone *)zone {
+ if (_sharedLayoutManager) {
+ return [_sharedLayoutManager retain];
+ } else {
+ return [super allocWithZone:zone];
+ }
+}
+
+- (id) init {
+ if (!_sharedLayoutManager) {
+ if (self = [super init]) {
+ //initialization goes here
+ isProcessingChanges = NO;
+ viewsToProcess = [[NSMutableArray alloc] init];
+ processedViews = [[NSMutableSet alloc] init];
+
+ hasRegistered = NO;
+ }
+ } else if (self != _sharedLayoutManager) {
+ [super dealloc];
+ self = _sharedLayoutManager;
+ }
+ return self;
+}
+
+- (void) dealloc {
+ [viewsToProcess release];
+ [processedViews release];
+ [super dealloc];
+}
+
+- (void) processView:(NSView *)aView {
+ if (hasRegistered == NO) {
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(frameChanged:) name:NSViewFrameDidChangeNotification object:nil];
+ hasRegistered = YES;
+ }
+ [processedViews addObject:aView];
+
+ /**
+ ORDER OF OPERATIONS:
+ 1. See if this view has any direct constraints
+ 2. See if this view has any siblings with constraints to this view
+ 3. See if this view has any children with constraints to superview
+
+ **/
+
+ //constraints for this view:
+ NSArray * constraints = [aView constraints];
+ for (CHLayoutConstraint * constraint in constraints) {
+ [constraint applyToTargetView:aView];
+ }
+
+ NSArray * superSubviews = [[aView superview] subviews];
+ for (NSView * subview in superSubviews) {
+ if (subview == aView) { continue; }
+
+ NSArray * subviewConstraints = [subview constraints];
+ for (CHLayoutConstraint * subviewConstraint in subviewConstraints) {
+ [subviewConstraint applyToTargetView:subview];
+ }
+ }
+
+ NSArray * subviews = [aView subviews];
+ for (NSView * subview in subviews) {
+ NSArray * subviewConstraints = [subview constraints];
+ for (CHLayoutConstraint * subviewConstraint in subviewConstraints) {
+ [subviewConstraint applyToTargetView:subview];
+ }
+ }
+}
+
+- (void) beginProcessingView:(NSView *)view {
+ if (isProcessingChanges == NO) {
+ isProcessingChanges = YES;
+ [viewsToProcess removeAllObjects];
+ [processedViews removeAllObjects];
+ [viewsToProcess addObject:view];
+
+ while([viewsToProcess count] > 0) {
+ NSView * currentView = [[viewsToProcess objectAtIndex:0] retain];
+ [viewsToProcess removeObjectAtIndex:0];
+ if ([viewsToProcess containsObject:currentView] == NO) {
+ [self processView:currentView];
+ }
+ [currentView release];
+ }
+
+ isProcessingChanges = NO;
+ } else {
+ if ([processedViews containsObject:view] == NO) {
+ [viewsToProcess addObject:view];
+ }
+ }
+}
+
+- (void) frameChanged:(NSNotification *)notification {
+ NSView * view = [notification object];
+ [self beginProcessingView:view];
+}
+
+@end
302 CHLayoutManager.xcodeproj/project.pbxproj
@@ -0,0 +1,302 @@
+// !$*UTF8*$!
+{
+ archiveVersion = 1;
+ classes = {
+ };
+ objectVersion = 45;
+ objects = {
+
+/* Begin PBXBuildFile section */
+ 1DDD58160DA1D0A300B32029 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1DDD58140DA1D0A300B32029 /* MainMenu.xib */; };
+ 256AC3DA0F4B6AC300CF3369 /* CHLayoutManagerAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 256AC3D90F4B6AC300CF3369 /* CHLayoutManagerAppDelegate.m */; };
+ 558686A31215C7910014B300 /* CHLayoutManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 558686A21215C7910014B300 /* CHLayoutManager.m */; };
+ 558686A61215CBD40014B300 /* CHLayoutConstraint.m in Sources */ = {isa = PBXBuildFile; fileRef = 558686A51215CBD40014B300 /* CHLayoutConstraint.m */; };
+ 558686C21215D0C40014B300 /* NSView+CHLayout.m in Sources */ = {isa = PBXBuildFile; fileRef = 558686C11215D0C40014B300 /* NSView+CHLayout.m */; };
+ 8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 089C165CFE840E0CC02AAC07 /* InfoPlist.strings */; };
+ 8D11072D0486CEB800E47090 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; settings = {ATTRIBUTES = (); }; };
+ 8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; };
+/* End PBXBuildFile section */
+
+/* Begin PBXFileReference section */
+ 089C165DFE840E0CC02AAC07 /* English */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = English; path = English.lproj/InfoPlist.strings; sourceTree = "<group>"; };
+ 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = "<absolute>"; };
+ 13E42FB307B3F0F600E4EEF1 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = /System/Library/Frameworks/CoreData.framework; sourceTree = "<absolute>"; };
+ 1DDD58150DA1D0A300B32029 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/MainMenu.xib; sourceTree = "<group>"; };
+ 256AC3D80F4B6AC300CF3369 /* CHLayoutManagerAppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHLayoutManagerAppDelegate.h; sourceTree = "<group>"; };
+ 256AC3D90F4B6AC300CF3369 /* CHLayoutManagerAppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CHLayoutManagerAppDelegate.m; sourceTree = "<group>"; };
+ 256AC3F00F4B6AF500CF3369 /* CHLayoutManager_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHLayoutManager_Prefix.pch; sourceTree = "<group>"; };
+ 29B97316FDCFA39411CA2CEA /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
+ 29B97324FDCFA39411CA2CEA /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = /System/Library/Frameworks/AppKit.framework; sourceTree = "<absolute>"; };
+ 29B97325FDCFA39411CA2CEA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = /System/Library/Frameworks/Foundation.framework; sourceTree = "<absolute>"; };
+ 550C4453121663CC00E0002B /* CHLayout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHLayout.h; sourceTree = "<group>"; };
+ 558686A11215C7910014B300 /* CHLayoutManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHLayoutManager.h; sourceTree = "<group>"; };
+ 558686A21215C7910014B300 /* CHLayoutManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CHLayoutManager.m; sourceTree = "<group>"; };
+ 558686A41215CBD40014B300 /* CHLayoutConstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CHLayoutConstraint.h; sourceTree = "<group>"; };
+ 558686A51215CBD40014B300 /* CHLayoutConstraint.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CHLayoutConstraint.m; sourceTree = "<group>"; };
+ 558686C01215D0C40014B300 /* NSView+CHLayout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSView+CHLayout.h"; sourceTree = "<group>"; };
+ 558686C11215D0C40014B300 /* NSView+CHLayout.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSView+CHLayout.m"; sourceTree = "<group>"; };
+ 8D1107310486CEB800E47090 /* CHLayoutManager-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "CHLayoutManager-Info.plist"; sourceTree = "<group>"; };
+ 8D1107320486CEB800E47090 /* CHLayoutManager.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CHLayoutManager.app; sourceTree = BUILT_PRODUCTS_DIR; };
+/* End PBXFileReference section */
+
+/* Begin PBXFrameworksBuildPhase section */
+ 8D11072E0486CEB800E47090 /* Frameworks */ = {
+ isa = PBXFrameworksBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ 8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */,
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ };
+/* End PBXFrameworksBuildPhase section */
+
+/* Begin PBXGroup section */
+ 080E96DDFE201D6D7F000001 /* Classes */ = {
+ isa = PBXGroup;
+ children = (
+ 550C4453121663CC00E0002B /* CHLayout.h */,
+ 558686C01215D0C40014B300 /* NSView+CHLayout.h */,
+ 558686C11215D0C40014B300 /* NSView+CHLayout.m */,
+ 558686A11215C7910014B300 /* CHLayoutManager.h */,
+ 558686A21215C7910014B300 /* CHLayoutManager.m */,
+ 558686A41215CBD40014B300 /* CHLayoutConstraint.h */,
+ 558686A51215CBD40014B300 /* CHLayoutConstraint.m */,
+ 256AC3D80F4B6AC300CF3369 /* CHLayoutManagerAppDelegate.h */,
+ 256AC3D90F4B6AC300CF3369 /* CHLayoutManagerAppDelegate.m */,
+ );
+ name = Classes;
+ sourceTree = "<group>";
+ };
+ 1058C7A0FEA54F0111CA2CBB /* Linked Frameworks */ = {
+ isa = PBXGroup;
+ children = (
+ 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */,
+ );
+ name = "Linked Frameworks";
+ sourceTree = "<group>";
+ };
+ 1058C7A2FEA54F0111CA2CBB /* Other Frameworks */ = {
+ isa = PBXGroup;
+ children = (
+ 29B97324FDCFA39411CA2CEA /* AppKit.framework */,
+ 13E42FB307B3F0F600E4EEF1 /* CoreData.framework */,
+ 29B97325FDCFA39411CA2CEA /* Foundation.framework */,
+ );
+ name = "Other Frameworks";
+ sourceTree = "<group>";
+ };
+ 19C28FACFE9D520D11CA2CBB /* Products */ = {
+ isa = PBXGroup;
+ children = (
+ 8D1107320486CEB800E47090 /* CHLayoutManager.app */,
+ );
+ name = Products;
+ sourceTree = "<group>";
+ };
+ 29B97314FDCFA39411CA2CEA /* CHLayoutManager */ = {
+ isa = PBXGroup;
+ children = (
+ 080E96DDFE201D6D7F000001 /* Classes */,
+ 29B97315FDCFA39411CA2CEA /* Other Sources */,
+ 29B97317FDCFA39411CA2CEA /* Resources */,
+ 29B97323FDCFA39411CA2CEA /* Frameworks */,
+ 19C28FACFE9D520D11CA2CBB /* Products */,
+ );
+ name = CHLayoutManager;
+ sourceTree = "<group>";
+ };
+ 29B97315FDCFA39411CA2CEA /* Other Sources */ = {
+ isa = PBXGroup;
+ children = (
+ 256AC3F00F4B6AF500CF3369 /* CHLayoutManager_Prefix.pch */,
+ 29B97316FDCFA39411CA2CEA /* main.m */,
+ );
+ name = "Other Sources";
+ sourceTree = "<group>";
+ };
+ 29B97317FDCFA39411CA2CEA /* Resources */ = {
+ isa = PBXGroup;
+ children = (
+ 8D1107310486CEB800E47090 /* CHLayoutManager-Info.plist */,
+ 089C165CFE840E0CC02AAC07 /* InfoPlist.strings */,
+ 1DDD58140DA1D0A300B32029 /* MainMenu.xib */,
+ );
+ name = Resources;
+ sourceTree = "<group>";
+ };
+ 29B97323FDCFA39411CA2CEA /* Frameworks */ = {
+ isa = PBXGroup;
+ children = (
+ 1058C7A0FEA54F0111CA2CBB /* Linked Frameworks */,
+ 1058C7A2FEA54F0111CA2CBB /* Other Frameworks */,
+ );
+ name = Frameworks;
+ sourceTree = "<group>";
+ };
+/* End PBXGroup section */
+
+/* Begin PBXNativeTarget section */
+ 8D1107260486CEB800E47090 /* CHLayoutManager */ = {
+ isa = PBXNativeTarget;
+ buildConfigurationList = C01FCF4A08A954540054247B /* Build configuration list for PBXNativeTarget "CHLayoutManager" */;
+ buildPhases = (
+ 8D1107290486CEB800E47090 /* Resources */,
+ 8D11072C0486CEB800E47090 /* Sources */,
+ 8D11072E0486CEB800E47090 /* Frameworks */,
+ );
+ buildRules = (
+ );
+ dependencies = (
+ );
+ name = CHLayoutManager;
+ productInstallPath = "$(HOME)/Applications";
+ productName = CHLayoutManager;
+ productReference = 8D1107320486CEB800E47090 /* CHLayoutManager.app */;
+ productType = "com.apple.product-type.application";
+ };
+/* End PBXNativeTarget section */
+
+/* Begin PBXProject section */
+ 29B97313FDCFA39411CA2CEA /* Project object */ = {
+ isa = PBXProject;
+ buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "CHLayoutManager" */;
+ compatibilityVersion = "Xcode 3.1";
+ hasScannedForEncodings = 1;
+ mainGroup = 29B97314FDCFA39411CA2CEA /* CHLayoutManager */;
+ projectDirPath = "";
+ projectRoot = "";
+ targets = (
+ 8D1107260486CEB800E47090 /* CHLayoutManager */,
+ );
+ };
+/* End PBXProject section */
+
+/* Begin PBXResourcesBuildPhase section */
+ 8D1107290486CEB800E47090 /* Resources */ = {
+ isa = PBXResourcesBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ 8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */,
+ 1DDD58160DA1D0A300B32029 /* MainMenu.xib in Resources */,
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ };
+/* End PBXResourcesBuildPhase section */
+
+/* Begin PBXSourcesBuildPhase section */
+ 8D11072C0486CEB800E47090 /* Sources */ = {
+ isa = PBXSourcesBuildPhase;
+ buildActionMask = 2147483647;
+ files = (
+ 8D11072D0486CEB800E47090 /* main.m in Sources */,
+ 256AC3DA0F4B6AC300CF3369 /* CHLayoutManagerAppDelegate.m in Sources */,
+ 558686A31215C7910014B300 /* CHLayoutManager.m in Sources */,
+ 558686A61215CBD40014B300 /* CHLayoutConstraint.m in Sources */,
+ 558686C21215D0C40014B300 /* NSView+CHLayout.m in Sources */,
+ );
+ runOnlyForDeploymentPostprocessing = 0;
+ };
+/* End PBXSourcesBuildPhase section */
+
+/* Begin PBXVariantGroup section */
+ 089C165CFE840E0CC02AAC07 /* InfoPlist.strings */ = {
+ isa = PBXVariantGroup;
+ children = (
+ 089C165DFE840E0CC02AAC07 /* English */,
+ );
+ name = InfoPlist.strings;
+ sourceTree = "<group>";
+ };
+ 1DDD58140DA1D0A300B32029 /* MainMenu.xib */ = {
+ isa = PBXVariantGroup;
+ children = (
+ 1DDD58150DA1D0A300B32029 /* English */,
+ );
+ name = MainMenu.xib;
+ sourceTree = "<group>";
+ };
+/* End PBXVariantGroup section */
+
+/* Begin XCBuildConfiguration section */
+ C01FCF4B08A954540054247B /* Debug */ = {
+ isa = XCBuildConfiguration;
+ buildSettings = {
+ ALWAYS_SEARCH_USER_PATHS = NO;
+ COPY_PHASE_STRIP = NO;
+ GCC_DYNAMIC_NO_PIC = NO;
+ GCC_ENABLE_FIX_AND_CONTINUE = YES;
+ GCC_MODEL_TUNING = G5;
+ GCC_OPTIMIZATION_LEVEL = 0;
+ GCC_PRECOMPILE_PREFIX_HEADER = YES;
+ GCC_PREFIX_HEADER = CHLayoutManager_Prefix.pch;
+ INFOPLIST_FILE = "CHLayoutManager-Info.plist";
+ INSTALL_PATH = "$(HOME)/Applications";
+ PRODUCT_NAME = CHLayoutManager;
+ };
+ name = Debug;
+ };
+ C01FCF4C08A954540054247B /* Release */ = {
+ isa = XCBuildConfiguration;
+ buildSettings = {
+ ALWAYS_SEARCH_USER_PATHS = NO;
+ DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
+ GCC_MODEL_TUNING = G5;
+ GCC_PRECOMPILE_PREFIX_HEADER = YES;
+ GCC_PREFIX_HEADER = CHLayoutManager_Prefix.pch;
+ INFOPLIST_FILE = "CHLayoutManager-Info.plist";
+ INSTALL_PATH = "$(HOME)/Applications";
+ PRODUCT_NAME = CHLayoutManager;
+ };
+ name = Release;
+ };
+ C01FCF4F08A954540054247B /* Debug */ = {
+ isa = XCBuildConfiguration;
+ buildSettings = {
+ ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
+ GCC_C_LANGUAGE_STANDARD = gnu99;
+ GCC_OPTIMIZATION_LEVEL = 0;
+ GCC_WARN_ABOUT_RETURN_TYPE = YES;
+ GCC_WARN_UNUSED_VARIABLE = YES;
+ ONLY_ACTIVE_ARCH = YES;
+ PREBINDING = NO;
+ SDKROOT = macosx10.6;
+ };
+ name = Debug;
+ };
+ C01FCF5008A954540054247B /* Release */ = {
+ isa = XCBuildConfiguration;
+ buildSettings = {
+ ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
+ GCC_C_LANGUAGE_STANDARD = gnu99;
+ GCC_WARN_ABOUT_RETURN_TYPE = YES;
+ GCC_WARN_UNUSED_VARIABLE = YES;
+ PREBINDING = NO;
+ SDKROOT = macosx10.6;
+ };
+ name = Release;
+ };
+/* End XCBuildConfiguration section */
+
+/* Begin XCConfigurationList section */
+ C01FCF4A08A954540054247B /* Build configuration list for PBXNativeTarget "CHLayoutManager" */ = {
+ isa = XCConfigurationList;
+ buildConfigurations = (
+ C01FCF4B08A954540054247B /* Debug */,
+ C01FCF4C08A954540054247B /* Release */,
+ );
+ defaultConfigurationIsVisible = 0;
+ defaultConfigurationName = Release;
+ };
+ C01FCF4E08A954540054247B /* Build configuration list for PBXProject "CHLayoutManager" */ = {
+ isa = XCConfigurationList;
+ buildConfigurations = (
+ C01FCF4F08A954540054247B /* Debug */,
+ C01FCF5008A954540054247B /* Release */,
+ );
+ defaultConfigurationIsVisible = 0;
+ defaultConfigurationName = Release;
+ };
+/* End XCConfigurationList section */
+ };
+ rootObject = 29B97313FDCFA39411CA2CEA /* Project object */;
+}
46 CHLayoutManagerAppDelegate.h
@@ -0,0 +1,46 @@
+//
+// CHLayoutManagerAppDelegate.h
+// CHLayoutManager
+/**
+ Copyright (c) 2010 Dave DeLong
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ **/
+
+#import <Cocoa/Cocoa.h>
+
+@interface CHLayoutManagerAppDelegate : NSObject <NSApplicationDelegate> {
+ NSWindow *window;
+
+ IBOutlet NSSlider * widthSlider;
+ IBOutlet NSButton * button1;
+ IBOutlet NSButton * button2;
+ IBOutlet NSProgressIndicator * progress;
+
+ IBOutlet NSButton * leftVerticalButton;
+ IBOutlet NSButton * rightVerticalButton;
+}
+
+@property (assign) IBOutlet NSWindow *window;
+
+- (IBAction) sliderChanged:(id)sender;
+
+- (IBAction) clickButton1:(id)sender;
+
+@end
72 CHLayoutManagerAppDelegate.m
@@ -0,0 +1,72 @@
+//
+// CHLayoutManagerAppDelegate.m
+// CHLayoutManager
+/**
+ Copyright (c) 2010 Dave DeLong
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ **/
+
+#import "CHLayoutManagerAppDelegate.h"
+#import "CHLayout.h"
+
+@implementation CHLayoutManagerAppDelegate
+
+@synthesize window;
+
+- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
+ // Insert code here to initialize your application
+
+ [button1 setLayoutName:@"button1"];
+ [button2 addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMinX relativeTo:@"button1" attribute:CHLayoutConstraintAttributeMaxX]];
+ [button2 addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMaxY relativeTo:@"button1" attribute:CHLayoutConstraintAttributeMaxY]];
+ [button2 addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeWidth relativeTo:@"button1" attribute:CHLayoutConstraintAttributeWidth]];
+
+ [progress startAnimation:nil];
+ CHLayoutConstraint * centerHorizontal = [CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMidX relativeTo:@"superview" attribute:CHLayoutConstraintAttributeMidX];
+ CHLayoutConstraint * centerVertical = [CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMidY relativeTo:@"superview" attribute:CHLayoutConstraintAttributeMidY];
+ [progress addConstraint:centerHorizontal];
+ [progress addConstraint:centerVertical];
+
+
+ [leftVerticalButton setLayoutName:@"leftVertical"];
+ [leftVerticalButton addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMinX relativeTo:@"superview" attribute:CHLayoutConstraintAttributeMinX offset:37]];
+ [rightVerticalButton addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMaxX relativeTo:@"superview" attribute:CHLayoutConstraintAttributeMaxX offset:-13]];
+ [rightVerticalButton addConstraint:[CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMinY relativeTo:@"leftVertical" attribute:CHLayoutConstraintAttributeMinY]];
+}
+
+- (IBAction) sliderChanged:(id)sender {
+ CGFloat sliderValue = [widthSlider floatValue];
+ NSRect button1Frame = [button1 frame];
+ button1Frame.size.width = sliderValue;
+ button1Frame.origin.y = sliderValue;
+ [button1 setFrame:button1Frame];
+
+ CGFloat range = ([widthSlider maxValue] - [widthSlider minValue]);
+ CGFloat percentage = ((sliderValue - range)/range) * 0.9;
+ NSRect leftVerticalFrame = [leftVerticalButton frame];
+ leftVerticalFrame.origin.y = ([[window contentView] frame].size.height * percentage);
+ [leftVerticalButton setFrame:leftVerticalFrame];
+}
+
+- (IBAction) clickButton1:(id)sender {
+
+}
+
+@end
7 CHLayoutManager_Prefix.pch
@@ -0,0 +1,7 @@
+//
+// Prefix header for all source files of the 'CHLayoutManager' target in the 'CHLayoutManager' project
+//
+
+#ifdef __OBJC__
+ #import <Cocoa/Cocoa.h>
+#endif
2  English.lproj/InfoPlist.strings
@@ -0,0 +1,2 @@
+/* Localized versions of Info.plist keys */
+
4,651 English.lproj/MainMenu.xib
4,651 additions, 0 deletions not shown
43 NSView+CHLayout.h
@@ -0,0 +1,43 @@
+//
+// NSView+CHLayout.h
+// CHLayoutManager
+/**
+ Copyright (c) 2010 Dave DeLong
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ **/
+
+#import <Cocoa/Cocoa.h>
+#import "CHLayoutConstraint.h"
+
+@interface NSView (CHLayout)
+
+- (void) setLayoutName:(NSString *)newLayoutName;
+- (NSString *) layoutName;
+
+- (void) addConstraint:(CHLayoutConstraint *)constraint;
+- (NSArray *) constraints;
+- (void) removeAllConstraints;
+
+- (CGFloat) valueForLayoutAttribute:(CHLayoutConstraintAttribute)attribute;
+- (void) setValue:(CGFloat)newValue forLayoutAttribute:(CHLayoutConstraintAttribute)attribute;
+
+- (NSView *) relativeViewForName:(NSString *)name;
+
+@end
136 NSView+CHLayout.m
@@ -0,0 +1,136 @@
+//
+// NSView+CHLayout.m
+// CHLayoutManager
+/**
+ Copyright (c) 2010 Dave DeLong
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
+ **/
+
+#import "NSView+CHLayout.h"
+#import <objc/runtime.h>
+#import "CHLayoutManager.h"
+
+static NSString * NSViewLayoutName_CHLayout = @"NSViewLayoutName_CHLayout";
+static NSString * NSViewConstraints_CHLayout = @"NSViewConstraints_CHLayout";
+
+@implementation NSView (CHLayout)
+
+- (void) setLayoutName:(NSString *)newLayoutName {
+ objc_setAssociatedObject(self, &NSViewLayoutName_CHLayout, newLayoutName, OBJC_ASSOCIATION_COPY);
+}
+
+- (NSString *) layoutName {
+ return (NSString *)objc_getAssociatedObject(self, &NSViewLayoutName_CHLayout);
+}
+
+- (void) addConstraint:(CHLayoutConstraint *)constraint {
+ NSMutableArray * constraints = (NSMutableArray *)objc_getAssociatedObject(self, &NSViewConstraints_CHLayout);
+ if (constraints == nil) {
+ constraints = [NSMutableArray array];
+ objc_setAssociatedObject(self, &NSViewConstraints_CHLayout, constraints, OBJC_ASSOCIATION_RETAIN);
+ }
+ [constraints addObject:constraint];
+ [[CHLayoutManager sharedLayoutManager] beginProcessingView:self];
+}
+
+- (NSArray *) constraints {
+ NSMutableArray * constraints = (NSMutableArray *)objc_getAssociatedObject(self, &NSViewConstraints_CHLayout);
+ if (constraints == nil) { return [NSArray array]; }
+
+ return [[constraints copy] autorelease];
+}
+
+- (void) removeAllConstraints {
+ NSMutableArray * constraints = (NSMutableArray *)objc_getAssociatedObject(self, &NSViewConstraints_CHLayout);
+ if (constraints != nil) {
+ [constraints removeAllObjects];
+ }
+}
+
+- (CGFloat) valueForLayoutAttribute:(CHLayoutConstraintAttribute)attribute {
+ NSRect frame = [self frame];
+ switch (attribute) {
+ case CHLayoutConstraintAttributeMinY:
+ return NSMinY(frame);
+ case CHLayoutConstraintAttributeMaxY:
+ return NSMaxY(frame);
+ case CHLayoutConstraintAttributeMinX:
+ return NSMinX(frame);
+ case CHLayoutConstraintAttributeMaxX:
+ return NSMaxX(frame);
+ case CHLayoutConstraintAttributeWidth:
+ return NSWidth(frame);
+ case CHLayoutConstraintAttributeHeight:
+ return NSHeight(frame);
+ case CHLayoutConstraintAttributeMidY:
+ return NSMidY(frame);
+ case CHLayoutConstraintAttributeMidX:
+ return NSMidX(frame);
+ default:
+ return 0;
+ }
+}
+
+- (void) setValue:(CGFloat)newValue forLayoutAttribute:(CHLayoutConstraintAttribute)attribute {
+ NSRect frame = [self frame];
+ switch (attribute) {
+ case CHLayoutConstraintAttributeMinY:
+ frame.origin.y = newValue;
+ break;
+ case CHLayoutConstraintAttributeMaxY:
+ frame.origin.y = newValue - frame.size.height;
+ break;
+ case CHLayoutConstraintAttributeMinX:
+ frame.origin.x = newValue;
+ break;
+ case CHLayoutConstraintAttributeMaxX:
+ frame.origin.x = newValue - frame.size.height;
+ break;
+ case CHLayoutConstraintAttributeWidth:
+ frame.size.width = newValue;
+ break;
+ case CHLayoutConstraintAttributeHeight:
+ frame.size.height = newValue;
+ break;
+ case CHLayoutConstraintAttributeMidY:
+ frame.origin.y = newValue - (frame.size.height/2);
+ break;
+ case CHLayoutConstraintAttributeMidX:
+ frame.origin.x = newValue - (frame.size.width/2);
+ break;
+ }
+ [self setFrame:frame];
+}
+
+- (NSView *) relativeViewForName:(NSString *)name {
+ if ([name isEqual:@"superview"]) {
+ return [self superview];
+ }
+
+ NSArray * superSubviews = [[self superview] subviews];
+ for (NSView *view in superSubviews) {
+ if ([[view layoutName] isEqual:name]) {
+ return (view == self ? nil : view);
+ }
+ }
+ return nil;
+}
+
+@end
58 README.markdown
@@ -0,0 +1,58 @@
+#CHLayoutManager
+
+CHLayoutManager is a way to add positioning and sizing constraints on views. The easiest way to understand this is with an example:
+
+##Example
+
+ [okButton setLayoutName:@"ok"];
+ CHLayoutConstraint * stayToTheLeft = [CHLayoutConstraint constraintWithAttribute:CHLayoutConstraintAttributeMaxX relativeTo:@"ok" attribute:CHLayoutConstraintAttributeMinX offset:-10];
+ [cancelButton addConstraint:stayToTheLeft];
+
+Now, whenever `okButton` is resized or moved, `cancelButton` will be automatically moved to have its right edge positioned 10 pixels to the left of `okButton`'s left edge.
+
+##Usage
+
+In order to use CHLayoutManager in your app, copy these 7 files into your project:
+
+- `CHLayout.h`
+- `CHLayoutManager.h`
+- `CHLayoutManager.m`
+- `CHLayoutConstraint.h`
+- `CHLayoutConstraint.m`
+- `NSView+CHLayout.h`
+- `NSView+CHLayout.m`
+
+Then `#import "CHLayout.h"` in any .m file that needs to apply constraints to views.
+
+##Supported Platforms
+
+- Mac OS X 10.5+ (modern runtime only)
+
+##Special Considerations
+
+- It's possible to set up circular dependencies on constraints. Do so at your own risk.
+- In order to constrain a view to its superview, create a constraint with the `sourceName` of `@"superview"`.
+
+##License
+
+CHLayoutManager is licensed under the MIT license, which is reproduced in its entirety here:
+
+>Copyright (c) 2010 Dave DeLong
+>
+>Permission is hereby granted, free of charge, to any person obtaining a copy
+>of this software and associated documentation files (the "Software"), to deal
+>in the Software without restriction, including without limitation the rights
+>to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+>copies of the Software, and to permit persons to whom the Software is
+>furnished to do so, subject to the following conditions:
+>
+>The above copyright notice and this permission notice shall be included in
+>all copies or substantial portions of the Software.
+>
+>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+>IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+>FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+>AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+>LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+>OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+>THE SOFTWARE.
14 main.m
@@ -0,0 +1,14 @@
+//
+// main.m
+// CHLayoutManager
+//
+// Created by Dave DeLong on 8/13/10.
+// Copyright 2010 Home. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+
+int main(int argc, char *argv[])
+{
+ return NSApplicationMain(argc, (const char **) argv);
+}
Please sign in to comment.
Something went wrong with that request. Please try again.