libui/redo/osxaltest/box.m

120 lines
2.7 KiB
Mathematica
Raw Normal View History

// 31 july 2015
#import "osxaltest.h"
2015-08-01 13:22:45 -05:00
@implementation tBox {
NSMutableArray *children;
NSMutableArray *stretchy;
NSView *sv;
2015-08-01 01:16:35 -05:00
BOOL vertical;
id<tControl> parent;
}
2015-08-01 13:22:45 -05:00
- (id)tInitVertical:(BOOL)vert
{
self = [super init];
if (self) {
self->children = [NSMutableArray new];
self->stretchy = [NSMutableArray new];
self->sv = nil;
2015-08-01 01:16:35 -05:00
self->vertical = vert;
self->parent = nil;
}
return self;
}
2015-08-01 13:22:45 -05:00
- (void)tAddControl:(id<tControl>)c stretchy:(BOOL)s
{
if (self->sv != nil)
[c tSetParent:self addToView:self->sv relayout:NO];
[self->children addObject:c];
[self->stretchy addObject:[NSNumber numberWithBool:s]];
// TODO mark as needing relayout
[self tRelayout];
}
- (void)tSetParent:(id<tControl>)p addToView:(NSView *)v relayout:(BOOL)relayout
{
self->parent = p;
self->sv = v;
2015-08-01 01:16:35 -05:00
[self->children enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
id<tControl> c;
2015-08-01 01:16:35 -05:00
c = (id<tControl>) obj;
[c tSetParent:self addToView:self->sv relayout:NO];
2015-08-01 01:16:35 -05:00
}];
if (relayout)
[self tRelayout];
}
// TODO MASSIVE CLEANUP and comments everywhere too
- (void)tFillAutoLayout:(tAutoLayoutParams *)p
2015-08-01 01:16:35 -05:00
{
NSMutableDictionary *views;
__block uintmax_t n;
tAutoLayoutParams pp;
__block BOOL anyStretchy;
NSMutableString *constraint;
2015-08-01 01:16:35 -05:00
views = [NSMutableDictionary new];
n = 0;
anyStretchy = NO;
[self->children enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop) {
id<tControl> c;
2015-08-02 11:52:24 -05:00
NSNumber *isStretchy;
2015-08-01 01:16:35 -05:00
c = (id<tControl>) obj;
isStretchy = (NSNumber *) [self->stretchy objectAtIndex:index];
if ([isStretchy boolValue])
anyStretchy = YES;
[c tFillAutoLayout:&pp];
[views setObjject:pp.view forKey:tAutoLayoutKey(n)];
n++;
}];
// first string the views together
constraint = [NSMutableString stringWithString:@"|"];
for (i = 0; i < n; i++) {
[constraint appendString:@"["];
[constraint appendString:tAutoLayoutKey(n)];
[constraint appendString:@"]"];
}
[constraint appendString:@"|"];
// TODO apply constraint
[constraint release];
// next make the views span the full other dimension
for (i = 0; i < n; i++) {
if (self->vertical)
constraint = [NSMutableString stringWithString:@"H:|"];
else
constraint = [NSMutableString stringWithString:@"V:|"];
[constraint appendString:tAutoLayoutKey(n)];
[constraint appendString:@"]|"];
// TODO apply constraint
[constraint release];
}
[views release];
// and now populate for self
p->view = TODO;
p->attachLeft = YES;
p->attachTop = YES;
// don't attach to the end if there weren't any stretchy controls
2015-08-02 11:22:24 -05:00
if (self->vertical) {
p->attachRight = YES;
p->attachBottom = anyStretchy;
} else {
p->attachRight = anyStretchy;
p->attachBottom = YES;
2015-08-02 11:22:24 -05:00
}
2015-08-01 01:16:35 -05:00
}
- (void)tRelayout
{
if (self->parent != nil)
[self->parent tRelayout];
}
@end