Changed tWindow to use the single-view-per-control model.

This commit is contained in:
Pietro Gagliardi 2015-08-02 21:34:54 -04:00
parent 90cd19fca6
commit bdf3bed5f4
2 changed files with 39 additions and 63 deletions

View File

@ -7,24 +7,11 @@ typedef struct tAutoLayoutParams tAutoLayoutParams;
// TODO stretchy across both dimensions
// for a vertical box, the horizontal width should be variable
struct tAutoLayoutParams {
NSMutableArray *horz;
NSMutableArray *horzAttachLeft;
NSMutableArray *horzAttachRight;
BOOL horzFirst;
BOOL horzLast;
NSMutableArray *vert;
NSMutableArray *vertAttachTop;
NSMutableArray *vertAttachBottom;
BOOL vertFirst;
BOOL vertLast;
NSMutableDictionary *views;
uintmax_t n;
BOOL horzStretchy;
BOOL horzFirstStretchy;
uintmax_t horzStretchyTo;
BOOL vertStretchy;
BOOL vertFirstStretchy;
uintmax_t vertStretchyTo;
NSView *view;
BOOL attachLeft;
BOOL attachTop;
BOOL attachRight;
BOOL attachBottom;
};
@protocol tControl

View File

@ -43,8 +43,10 @@
{
NSView *contentView;
tAutoLayoutParams p;
NSDictionary *views;
NSString *margin;
void (^run)(NSArray *, NSArray *, NSArray *, NSString *);
NSMutableString *constraint;
NSArray *constraints;
if (self->c == nil)
return;
@ -52,58 +54,45 @@
contentView = [self->w contentView];
[contentView removeConstraints:[contentView constraints]];
p.horz = [NSMutableArray new];
p.horzAttachLeft = [NSMutableArray new];
p.horzAttachRight = [NSMutableArray new];
p.horzFirst = YES; // only control here
p.horzLast = YES;
p.vert = [NSMutableArray new];
p.vertAttachTop = [NSMutableArray new];
p.vertAttachBottom = [NSMutableArray new];
p.vertFirst = YES;
p.vertLast = YES;
p.views = [NSMutableDictionary new];
p.n = 0;
p.horzStretchy = YES; // assumption for the sole control to avoid fixed size hacks
p.horzFirstStretchy = YES;
p.vertStretchy = YES;
p.vertFirstStretchy = YES;
[self->c tFillAutoLayout:&p];
views = [NSDictionary dictionaryWithObject:p.view forKey:@"view"];
margin = @"";
if (self->margined)
margin = @"-";
run = ^(NSArray *side, NSArray *attachStart, NSArray *attachEnd, NSString *prefix) {
NSUInteger i;
// TODO always append margins even if not attached?
// or if not attached, append ->=0- as well?
constraint = [NSMutableString stringWithString:@"H:"];
if (p.attachLeft) {
[constraint appendString:@"|"];
[constraint appendString:margin];
}
[constraint appendString:@"[view]"];
if (p.attachRight) {
[constraint appendString:margin];
[constraint appendString:@"|"];
}
constraints = [NSLayoutConstraint constraintsWithVisualFormat:constraint options:0 metrics:nil views:p.views];
[contentView addConstraints:constraints];
[constraint release];
for (i = 0; i < [side count]; i++) {
NSMutableString *constraint;
NSNumber *attach;
NSArray *constraints;
constraint = [NSMutableString stringWithString:@"V:"];
if (p.attachTop) {
[constraint appendString:@"|"];
[constraint appendString:margin];
}
[constraint appendString:@"[view]"];
if (p.attachBottom) {
[constraint appendString:margin];
[constraint appendString:@"|"];
}
constraints = [NSLayoutConstraint constraintsWithVisualFormat:constraint options:0 metrics:nil views:p.views];
[contentView addConstraints:constraints];
[constraint release];
constraint = [NSMutableString stringWithString:prefix];
attach = (NSNumber *) [attachStart objectAtIndex:i];
if ([attach boolValue]) {
[constraint appendString:@"|"];
[constraint appendString:margin];
}
[constraint appendString:[side objectAtIndex:i]];
attach = (NSNumber *) [attachEnd objectAtIndex:i];
if ([attach boolValue]) {
[constraint appendString:margin];
[constraint appendString:@"|"];
}
constraints = [NSLayoutConstraint constraintsWithVisualFormat:constraint options:0 metrics:nil views:p.views];
[contentView addConstraints:constraints];
//TODO uncomment this and the program tries to access after free [constraint release];
}
};
run(p.horz, p.horzAttachLeft, p.horzAttachRight, @"H:");
run(p.vert, p.vertAttachTop, p.vertAttachBottom, @"V:");
// TODO release everything
[views release];
}
@end