libui/redo/osxaltest/box.m

134 lines
3.2 KiB
Mathematica
Raw Normal View History

// 31 july 2015
#import "osxaltest.h"
2015-08-02 21:38:24 -05:00
// 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
2015-08-01 13:22:45 -05:00
@implementation tBox {
2015-08-02 21:38:24 -05:00
NSView *v;
NSMutableArray *children;
NSMutableArray *stretchy;
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) {
2015-08-02 21:38:24 -05:00
self->v = [[NSView alloc] initWithFrame:NSZeroRect];
[self->v setTranslatesAutoresizingMaskIntoConstraints:NO];
self->children = [NSMutableArray new];
self->stretchy = [NSMutableArray new];
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
{
2015-08-02 21:38:24 -05:00
[c tSetParent:self addToView:self->v relayout:NO];
[self->children addObject:c];
[self->stretchy addObject:[NSNumber numberWithBool:s]];
[self tRelayout];
}
2015-08-02 21:38:24 -05:00
- (void)tSetParent:(id<tControl>)p addToView:(NSView *)sv relayout:(BOOL)relayout
{
self->parent = p;
2015-08-02 21:38:24 -05:00
[sv addSubview:self->v];
if (relayout)
[self tRelayout];
}
2015-08-02 21:38:24 -05:00
// TODO stretchy
// TODO spaced
- (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
2015-08-02 21:38:24 -05:00
[self->v removeConstraints:[self->v constraints]];
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
2015-08-02 21:38:24 -05:00
if (self->vertical)
constraint = [NSMutableString stringWithString:@"V:|"];
else
constraint = [NSMutableString stringWithString:@"H:|"];
for (i = 0; i < n; i++) {
[constraint appendString:@"["];
[constraint appendString:tAutoLayoutKey(n)];
[constraint appendString:@"]"];
}
[constraint appendString:@"|"];
2015-08-02 21:38:24 -05:00
[self->v addConstraint:[NSLayoutConstraint constraintsWithVisualFormat:constraint options:0 metrics:nil views:views]];
[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:@"]|"];
2015-08-02 21:38:24 -05:00
[self->v addConstraint:[NSLayoutConstraint constraintsWithVisualFormat:constraint options:0 metrics:nil views:views]];
[constraint release];
}
[views release];
// and now populate for self
2015-08-02 21:38:24 -05:00
p->view = self->v;
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