diff --git a/MathPad/MPExpressionLayout.h b/MathPad/MPExpressionLayout.h index decb929..7e9a8f9 100644 --- a/MathPad/MPExpressionLayout.h +++ b/MathPad/MPExpressionLayout.h @@ -9,10 +9,12 @@ @import Cocoa; #import "MPLayout.h" #import "MPExpression.h" +#import "MPFunctionLayout.h" @interface MPExpressionLayout : MPLayout -- (instancetype)initRootLayoutWithExpression:(MPExpression *)expression; +- (instancetype)initWithExpression:(MPExpression *)expression + parent:(MPFunctionLayout *)parent; @property (readonly, nonatomic, weak) MPExpression *expression; diff --git a/MathPad/MPExpressionLayout.m b/MathPad/MPExpressionLayout.m index ccb008a..47a3220 100644 --- a/MathPad/MPExpressionLayout.m +++ b/MathPad/MPExpressionLayout.m @@ -41,36 +41,23 @@ @implementation MPExpressionLayout # pragma mark Creation Methods -- (instancetype)initRootLayoutWithExpression:(MPExpression *)expression +- (instancetype)initWithExpression:(MPExpression *)expression + parent:(MPFunctionLayout *)parent { - self = [super init]; + self = [super initWithParent:parent]; if (self) { _expression = expression; } return self; } -- (instancetype)initWithElementAtPath:(NSIndexPath *)path - inRootExpression:(MPExpression *)rootExpression - parent:(MPLayout *)parent -{ - self = [super initWithElementAtPath:path - inRootExpression:rootExpression - parent:parent]; - if (self) { - _expression = [rootExpression elementAtIndexPath:path]; - } - return self; -} - #pragma mark Cache Methods - (MPLayout *)childLayoutAtIndex:(NSUInteger)index { id cachedObject = [self cachableObjectForIndex:index generator:^id{ - NSIndexPath *indexPath = [self.expression.indexPath indexPathByAddingIndex:index]; - MPFunctionLayout *layout = [MPFunctionLayout functionLayoutForFunctionAtIndexPath:indexPath - inRootExpression:self.expression.rootExpression - parent:self]; + MPFunction *function = (MPFunction *)[self.expression elementAtIndex:index]; + MPFunctionLayout *layout = [MPFunctionLayout functionLayoutForFunction:function + parent:self]; layout.flipped = self.flipped; layout.usesSmallSize = self.usesSmallSize; return layout; @@ -114,7 +101,7 @@ - (NSRect)boundingRectForRange:(NSRange)range { NSUInteger startOffset; - NSUInteger startElementIndex = [self.expression indexOfElementAtSymbolLocation:range.location + NSUInteger startElementIndex = [self.expression indexOfElementAtLocation:range.location offset:&startOffset]; // Calculate x position CGFloat x = 0, width = 0; @@ -139,7 +126,7 @@ } NSUInteger endOffset; - NSUInteger endElementIndex = [self.expression indexOfElementAtSymbolLocation:NSMaxRange(range) + NSUInteger endElementIndex = [self.expression indexOfElementAtLocation:NSMaxRange(range) offset:&endOffset]; // Selection is inside of one string element diff --git a/MathPad/MPExpressionStorage.m b/MathPad/MPExpressionStorage.m index 0a771f5..872b8ff 100644 --- a/MathPad/MPExpressionStorage.m +++ b/MathPad/MPExpressionStorage.m @@ -24,7 +24,7 @@ { self = [super initWithElements:elements]; if (self) { - _rootLayout = [[MPExpressionLayout alloc] initRootLayoutWithExpression:self]; + _rootLayout = [[MPExpressionLayout alloc] initWithExpression:self parent:nil]; } return self; } @@ -32,11 +32,11 @@ - (void)setExpressionView:(MPExpressionView *)expressionView { _expressionView = expressionView; - self.rootLayout = [[MPExpressionLayout alloc] initRootLayoutWithExpression:self]; + self.rootLayout = [[MPExpressionLayout alloc] initWithExpression:self parent:nil]; self.rootLayout.flipped = expressionView.isFlipped; } -- (void)didChangeElementsInRangePath:(MPRangePath *)rangePath +- (void)didChangeElementsInIndexedRangePath:(MPRangePath *)rangePath replacementLength:(NSUInteger)replacementLength { if (rangePath.location.length == 0) { diff --git a/MathPad/MPExpressionView.h b/MathPad/MPExpressionView.h index 3da1ec9..eb68fe0 100644 --- a/MathPad/MPExpressionView.h +++ b/MathPad/MPExpressionView.h @@ -22,7 +22,7 @@ @property (readonly, nonatomic, strong) MPExpressionStorage *expressionStorage; -@property (nonatomic, getter = isEditable) BOOL editable; +// @property (nonatomic, getter = isEditable) BOOL editable; @property (nonatomic, strong) MPRangePath *selection; @property (nonatomic, weak) id target; diff --git a/MathPad/MPExpressionView.m b/MathPad/MPExpressionView.m index 79e2183..8390cee 100644 --- a/MathPad/MPExpressionView.m +++ b/MathPad/MPExpressionView.m @@ -112,7 +112,7 @@ MPExpression *targetExpression = [self.expressionStorage elementAtIndexPath:targetExpressionPath]; NSUInteger locationInTarget = selectionPath.lastIndex; NSUInteger locationInElement; - NSUInteger targetElementIndex = [targetExpression indexOfElementAtSymbolLocation:locationInTarget + NSUInteger targetElementIndex = [targetExpression indexOfElementAtLocation:locationInTarget offset:&locationInElement]; id targetElement; @@ -172,7 +172,7 @@ MPExpression *targetExpression = [self.expressionStorage elementAtIndexPath:targetExpressionPath]; NSUInteger locationInTarget = selectionPath.lastIndex; NSUInteger locationInElement; - NSUInteger targetElementIndex = [targetExpression indexOfElementAtSymbolLocation:locationInTarget + NSUInteger targetElementIndex = [targetExpression indexOfElementAtLocation:locationInTarget offset:&locationInElement]; NSUInteger previousElementIndex = targetElementIndex - (locationInElement == 0 ? 1 : 0); diff --git a/MathPad/MPFunctionLayout.h b/MathPad/MPFunctionLayout.h index 61d9840..3768f59 100644 --- a/MathPad/MPFunctionLayout.h +++ b/MathPad/MPFunctionLayout.h @@ -11,9 +11,8 @@ @interface MPFunctionLayout : MPLayout -+ (MPFunctionLayout *)functionLayoutForFunctionAtIndexPath:(NSIndexPath *)path - inRootExpression:(MPExpression *)rootExpression - parent:(MPExpressionLayout *)parent; ++ (MPFunctionLayout *)functionLayoutForFunction:(MPFunction *)function + parent:(MPExpressionLayout *)parent; @property (readonly, nonatomic, weak) MPFunction *function; @@ -21,6 +20,9 @@ @interface MPFunctionLayout (MPSubclassOverride) +- (instancetype)initWithFunction:(MPFunction *)function + parent:(MPExpressionLayout *)parent; + #pragma mark Cache Methods - (CTLineRef)lineForPrivateCacheIndex:(NSUInteger)index generator:(CTLineRef (^)())generator; diff --git a/MathPad/MPFunctionLayout.m b/MathPad/MPFunctionLayout.m index da17931..3484a26 100644 --- a/MathPad/MPFunctionLayout.m +++ b/MathPad/MPFunctionLayout.m @@ -16,31 +16,23 @@ @implementation MPFunctionLayout #pragma mark Creation Methods -+ (MPFunctionLayout *)functionLayoutForFunctionAtIndexPath:(NSIndexPath *)path - inRootExpression:(MPExpression *)rootExpression - parent:(MPExpressionLayout *)parent ++ (MPFunctionLayout *)functionLayoutForFunction:(MPFunction *)function + parent:(MPExpressionLayout *)parent { - MPFunction *function = [rootExpression elementAtIndexPath:path]; Class class = [function class]; if (class == [MPSumFunction class]) { - return [[MPSumFunctionLayout alloc] initWithElementAtPath:path - inRootExpression:rootExpression - parent:parent]; + return [[MPSumFunctionLayout alloc] initWithFunction:function parent:parent]; } - return [[self alloc] initWithElementAtPath:path - inRootExpression:rootExpression - parent:parent]; + return [[self alloc] initWithFunction:function + parent:parent]; } -- (instancetype)initWithElementAtPath:(NSIndexPath *)path - inRootExpression:(MPExpression *)rootExpression - parent:(MPLayout *)parent +- (instancetype)initWithFunction:(MPFunction *)function + parent:(MPExpressionLayout *)parent { - self = [super initWithElementAtPath:path - inRootExpression:rootExpression - parent:parent]; + self = [super initWithParent:parent]; if (self) { - _function = [rootExpression elementAtIndexPath:path]; + _function = function; } return self; } @@ -62,10 +54,9 @@ - (MPLayout *)childLayoutAtIndex:(NSUInteger)index { return [self cachableObjectForIndex:index generator:^id{ - NSIndexPath *childPath = [self.function.indexPath indexPathByAddingIndex:index]; - MPExpressionLayout *layout = [[MPExpressionLayout alloc] initWithElementAtPath:childPath - inRootExpression:self.function.rootExpression - parent:self]; + MPExpression *child = [self.function childAtIndex:index]; + MPExpressionLayout *layout = [[MPExpressionLayout alloc] initWithExpression:child + parent:self]; layout.flipped = self.flipped; layout.usesSmallSize = (index == 0 || index == 1) ? YES : self.usesSmallSize; return layout; diff --git a/MathPad/MPLayout.h b/MathPad/MPLayout.h index c211246..0eff489 100644 --- a/MathPad/MPLayout.h +++ b/MathPad/MPLayout.h @@ -15,9 +15,7 @@ #pragma mark Creation Methods - (instancetype)init; -- (instancetype)initWithElementAtPath:(NSIndexPath *)path - inRootExpression:(MPExpression *)rootExpression - parent:(MPLayout *)parent; +- (instancetype)initWithParent:(MPLayout *)parent; #pragma mark Properties - (NSFont *)font; diff --git a/MathPad/MPLayout.m b/MathPad/MPLayout.m index a7e9492..3904f62 100644 --- a/MathPad/MPLayout.m +++ b/MathPad/MPLayout.m @@ -35,9 +35,7 @@ return self; } -- (instancetype)initWithElementAtPath:(NSIndexPath *)path - inRootExpression:(MPExpression *)rootExpression - parent:(MPLayout *)parent +- (instancetype)initWithParent:(MPLayout *)parent { self = [self init]; if (self) {