Skip to content

Instantly share code, notes, and snippets.

@nilium
Created April 11, 2010 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nilium/f76ee354d50d336ddde3 to your computer and use it in GitHub Desktop.
Save nilium/f76ee354d50d336ddde3 to your computer and use it in GitHub Desktop.
//
// RLayoutAppDelegate.m
// RLayout
//
// Created by Noel Cower on 4/8/10.
// Copyright 2010 Noel Cower. All rights reserved.
//
#import "RLayoutAppDelegate.h"
@implementation RLayoutAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// create layout view
layout = [[[NLayoutView alloc] initWithFrame:[window.contentView bounds]] autorelease];
[window setContentView:layout];
[layout setAutoresizingMask: // since the layout view cannot do layout on itself,
NSViewMinXMargin|NSViewMaxXMargin|NSViewWidthSizable| // I have to use the abysmal anchor/spring stuff to
NSViewMinYMargin|NSViewMaxYMargin|NSViewHeightSizable]; // get it to autoresize with the window's contentview
// add draggable subview
horiz = [[[NDraggable alloc] initWithFrame:NSMakeRect(128, 128, 10, 10)] autorelease];
[layout addSubview:horiz];
[horiz setAutoresizingMask:NSViewMinYMargin|NSViewMaxYMargin];
[horiz setDelegate:self];
vert = [[[NDraggable alloc] initWithFrame:NSMakeRect(128, 128, 10, 10)] autorelease];
[layout addSubview:vert];
[vert setAutoresizingMask:NSViewMinXMargin|NSViewMaxXMargin];
[vert setDelegate:self];
center = [[[NDraggable alloc] initWithFrame:NSMakeRect(128, 128, 10, 10)] autorelease];
[center setAutoresizingMask:NSViewMinXMargin|NSViewMaxXMargin|NSViewMinYMargin|NSViewMaxYMargin];
[layout addSubview:center];
[center setDelegate:self];
[layout setLayoutParam:NLFillHorizontal forSubview:horiz withBase:nil];
[layout setLayoutParam:NLCenterVertical forSubview:horiz withBase:center];
[layout setLayoutParam:NLFillVertical forSubview:vert withBase:nil];
[layout setLayoutParam:NLCenterHorizontal forSubview:vert withBase:center];
// set up four button subviews
// upper left
NSButton *button = [[[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 64, 64)] autorelease];
[layout addSubview:button];
[layout setLayoutParam:NLLeftOf forSubview:button withBase:vert];
[layout setLayoutParam:NLAbove forSubview:button withBase:horiz];
[layout setLayoutParam:NLAlignLeft forSubview:button withBase:nil];
[layout setLayoutParam:NLAlignTop forSubview:button withBase:nil];
// upper right
button = [[[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 64, 64)] autorelease];
[layout addSubview:button];
[layout setLayoutParam:NLRightOf forSubview:button withBase:vert];
[layout setLayoutParam:NLAbove forSubview:button withBase:horiz];
[layout setLayoutParam:NLAlignRight forSubview:button withBase:nil];
[layout setLayoutParam:NLAlignTop forSubview:button withBase:nil];
// lower left
button = [[[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 64, 64)] autorelease];
[layout addSubview:button];
[layout setLayoutParam:NLLeftOf forSubview:button withBase:vert];
[layout setLayoutParam:NLBelow forSubview:button withBase:horiz];
[layout setLayoutParam:NLAlignLeft forSubview:button withBase:nil];
[layout setLayoutParam:NLAlignBottom forSubview:button withBase:nil];
// lower right
button = [[[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 64, 64)] autorelease];
[layout addSubview:button];
[layout setLayoutParam:NLRightOf forSubview:button withBase:vert];
[layout setLayoutParam:NLBelow forSubview:button withBase:horiz];
[layout setLayoutParam:NLAlignRight forSubview:button withBase:nil];
[layout setLayoutParam:NLAlignBottom forSubview:button withBase:nil];
// do initial layout
[layout performLayout];
}
- (void)draggableFrameDidChange:(NDraggable*)dragger {
// This would be a case of circular relations - that is, each draggable view depends on
// the other draggable views to update its position
if (dragger != center) {
NSRect to = [center frame];
if (dragger == vert) {
to.origin.x = [vert frame].origin.x;
} else if (dragger == horiz) {
to.origin.y = [horiz frame].origin.y;
}
[center setFrame:to];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment