2015-08-01 14:38:59 -05:00
|
|
|
// 1 august 2015
|
|
|
|
#include "osxaltest.h"
|
|
|
|
|
|
|
|
@implementation tWindow {
|
|
|
|
NSWindow *w;
|
|
|
|
id<tControl> c;
|
2015-08-01 14:44:06 -05:00
|
|
|
BOOL margined;
|
2015-08-01 14:38:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
|
|
|
self->w = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 320, 240)
|
|
|
|
styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask)
|
|
|
|
backing:NSBackingStoreBuffered
|
|
|
|
defer:YES];
|
|
|
|
[self->w setTitle:@"Auto Layout Test"];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)tSetControl:(id<tControl>)cc
|
|
|
|
{
|
|
|
|
self->c = cc;
|
2015-08-01 20:41:36 -05:00
|
|
|
[self->c tSetParent:self addToView:[self->w contentView]];
|
2015-08-01 14:38:59 -05:00
|
|
|
[self tRelayout];
|
|
|
|
}
|
|
|
|
|
2015-08-01 14:44:06 -05:00
|
|
|
- (void)tSetMargined:(BOOL)m
|
|
|
|
{
|
|
|
|
self->margined = m;
|
|
|
|
[self tRelayout];
|
|
|
|
}
|
|
|
|
|
2015-08-01 14:38:59 -05:00
|
|
|
- (void)tShow
|
|
|
|
{
|
|
|
|
[self->w cascadeTopLeftFromPoint:NSMakePoint(20, 20)];
|
|
|
|
[self->w makeKeyAndOrderFront:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)tRelayout
|
|
|
|
{
|
|
|
|
NSView *contentView;
|
2015-08-01 16:26:56 -05:00
|
|
|
NSMutableArray *horz, *vert;
|
2015-08-01 14:38:59 -05:00
|
|
|
NSMutableArray *extra, *extraVert;
|
|
|
|
NSMutableDictionary *views;
|
2015-08-01 16:26:56 -05:00
|
|
|
NSUInteger i;
|
2015-08-01 14:44:06 -05:00
|
|
|
NSString *margin;
|
2015-08-01 17:05:50 -05:00
|
|
|
uintmax_t n;
|
2015-08-01 14:38:59 -05:00
|
|
|
|
2015-08-01 14:44:06 -05:00
|
|
|
if (self->c == nil)
|
|
|
|
return;
|
2015-08-01 14:38:59 -05:00
|
|
|
contentView = [self->w contentView];
|
|
|
|
[contentView removeConstraints:[contentView constraints]];
|
2015-08-01 16:26:56 -05:00
|
|
|
horz = [NSMutableArray new];
|
|
|
|
vert = [NSMutableArray new];
|
2015-08-01 14:38:59 -05:00
|
|
|
extra = [NSMutableArray new];
|
|
|
|
extraVert = [NSMutableArray new];
|
|
|
|
views = [NSMutableDictionary new];
|
2015-08-01 17:05:50 -05:00
|
|
|
n = 0;
|
|
|
|
[self->c tFillAutoLayoutHorz:horz vert:vert extra:extra extraVert:extraVert views:views first:&n];
|
2015-08-01 14:44:06 -05:00
|
|
|
margin = @"";
|
|
|
|
if (self->margined)
|
|
|
|
margin = @"-";
|
2015-08-01 16:26:56 -05:00
|
|
|
[horz enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
|
|
|
|
[extra addObject:[NSString stringWithFormat:@"|%@%@%@|", margin, obj, margin]];
|
|
|
|
[extraVert addObject:@NO];
|
|
|
|
}];
|
|
|
|
[vert enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
|
|
|
|
[extra addObject:[NSString stringWithFormat:@"|%@%@%@|", margin, obj, margin]];
|
|
|
|
[extraVert addObject:@YES];
|
|
|
|
}];
|
2015-08-01 14:38:59 -05:00
|
|
|
for (i = 0; i < [extra count]; i++) {
|
|
|
|
NSString *constraint;
|
|
|
|
NSNumber *vertical;
|
|
|
|
NSArray *constraints;
|
|
|
|
|
|
|
|
vertical = (NSNumber *) [extraVert objectAtIndex:i];
|
|
|
|
if ([vertical boolValue])
|
|
|
|
constraint = [NSString stringWithFormat:@"V:%@", [extra objectAtIndex:i]];
|
|
|
|
else
|
|
|
|
constraint = [NSString stringWithFormat:@"H:%@", [extra objectAtIndex:i]];
|
|
|
|
constraints = [NSLayoutConstraint constraintsWithVisualFormat:constraint options:0 metrics:nil views:views];
|
|
|
|
[contentView addConstraints:constraints];
|
|
|
|
}
|
|
|
|
// TODO release everything
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|