Modified box.m to use a single NSView.

This commit is contained in:
Pietro Gagliardi 2015-08-02 22:38:24 -04:00
parent f275a9a043
commit bd166e601f
1 changed files with 31 additions and 18 deletions

View File

@ -1,10 +1,25 @@
// 31 july 2015 // 31 july 2015
#import "osxaltest.h" #import "osxaltest.h"
// leave a whole lot of space around the alignment rect, just to be safe
// TODO fine tune this
// TODO de-duplicate this from spinbox.m
@interface tBoxContainer : NSView
@end
@implementation tBoxContainer
- (NSEdgeInsets)alignmentRectInsets
{
return NSEdgeInsetsMake(50, 50, 50, 50);
}
@end
@implementation tBox { @implementation tBox {
NSView *v;
NSMutableArray *children; NSMutableArray *children;
NSMutableArray *stretchy; NSMutableArray *stretchy;
NSView *sv;
BOOL vertical; BOOL vertical;
id<tControl> parent; id<tControl> parent;
} }
@ -13,9 +28,9 @@
{ {
self = [super init]; self = [super init];
if (self) { if (self) {
self->v = [[NSView alloc] initWithFrame:NSZeroRect];
self->children = [NSMutableArray new]; self->children = [NSMutableArray new];
self->stretchy = [NSMutableArray new]; self->stretchy = [NSMutableArray new];
self->sv = nil;
self->vertical = vert; self->vertical = vert;
self->parent = nil; self->parent = nil;
} }
@ -24,29 +39,22 @@
- (void)tAddControl:(id<tControl>)c stretchy:(BOOL)s - (void)tAddControl:(id<tControl>)c stretchy:(BOOL)s
{ {
if (self->sv != nil) [c tSetParent:self addToView:self->v relayout:NO];
[c tSetParent:self addToView:self->sv relayout:NO];
[self->children addObject:c]; [self->children addObject:c];
[self->stretchy addObject:[NSNumber numberWithBool:s]]; [self->stretchy addObject:[NSNumber numberWithBool:s]];
// TODO mark as needing relayout
[self tRelayout]; [self tRelayout];
} }
- (void)tSetParent:(id<tControl>)p addToView:(NSView *)v relayout:(BOOL)relayout - (void)tSetParent:(id<tControl>)p addToView:(NSView *)sv relayout:(BOOL)relayout
{ {
self->parent = p; self->parent = p;
self->sv = v; [sv addSubview:self->v];
[self->children enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
id<tControl> c;
c = (id<tControl>) obj;
[c tSetParent:self addToView:self->sv relayout:NO];
}];
if (relayout) if (relayout)
[self tRelayout]; [self tRelayout];
} }
// TODO MASSIVE CLEANUP and comments everywhere too // TODO stretchy
// TODO spaced
- (void)tFillAutoLayout:(tAutoLayoutParams *)p - (void)tFillAutoLayout:(tAutoLayoutParams *)p
{ {
NSMutableDictionary *views; NSMutableDictionary *views;
@ -55,6 +63,8 @@
__block BOOL anyStretchy; __block BOOL anyStretchy;
NSMutableString *constraint; NSMutableString *constraint;
[self->v removeConstraints:[self->v constraints]];
views = [NSMutableDictionary new]; views = [NSMutableDictionary new];
n = 0; n = 0;
anyStretchy = NO; anyStretchy = NO;
@ -72,14 +82,17 @@
}]; }];
// first string the views together // first string the views together
constraint = [NSMutableString stringWithString:@"|"]; if (self->vertical)
constraint = [NSMutableString stringWithString:@"V:|"];
else
constraint = [NSMutableString stringWithString:@"H:|"];
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
[constraint appendString:@"["]; [constraint appendString:@"["];
[constraint appendString:tAutoLayoutKey(n)]; [constraint appendString:tAutoLayoutKey(n)];
[constraint appendString:@"]"]; [constraint appendString:@"]"];
} }
[constraint appendString:@"|"]; [constraint appendString:@"|"];
// TODO apply constraint [self->v addConstraint:[NSLayoutConstraint constraintsWithVisualFormat:constraint options:0 metrics:nil views:views]];
[constraint release]; [constraint release];
// next make the views span the full other dimension // next make the views span the full other dimension
@ -90,14 +103,14 @@
constraint = [NSMutableString stringWithString:@"V:|"]; constraint = [NSMutableString stringWithString:@"V:|"];
[constraint appendString:tAutoLayoutKey(n)]; [constraint appendString:tAutoLayoutKey(n)];
[constraint appendString:@"]|"]; [constraint appendString:@"]|"];
// TODO apply constraint [self->v addConstraint:[NSLayoutConstraint constraintsWithVisualFormat:constraint options:0 metrics:nil views:views]];
[constraint release]; [constraint release];
} }
[views release]; [views release];
// and now populate for self // and now populate for self
p->view = TODO; p->view = self->v;
p->attachLeft = YES; p->attachLeft = YES;
p->attachTop = YES; p->attachTop = YES;
// don't attach to the end if there weren't any stretchy controls // don't attach to the end if there weren't any stretchy controls