Archived
1

Added Lots of Documentation

Added some nice to haves
Improved and Unified General Code Layout
This commit is contained in:
Kim Wittenburg
2015-01-04 02:54:27 +01:00
parent 152b981e24
commit 7438fd1f95
83 changed files with 2282 additions and 416 deletions

View File

@@ -8,26 +8,25 @@
#import "MPExpressionView.h"
#import "MPExpression.h"
#import "MPExpression.h"
#import "MPParsedExpression.h"
#import "MPPowerFunction.h"
#import "MPParenthesisFunction.h"
#import "MPFractionFunction.h"
#import "MPToken.h"
#import "MPRangePath.h"
#import "MPMathRules.h"
#import "MPExpressionStorage.h"
#import "MPExpressionLayout.h"
#import "MPFunctionLayout.h"
#import "MPFunctionsViewController.h"
#import "MPFunctionLayout.h"
#import "MPFractionFunction.h"
#import "MPParenthesisFunction.h"
#import "MPPowerFunction.h"
#import "MPParsedExpression.h"
#import "MPMathRules.h"
#import "MPToken.h"
#import "MPRangePath.h"
#import "NSIndexPath+MPAdditions.h"
@interface MPExpressionView ()
@property (nonatomic, strong) NSButton *functionsButton;
@@ -58,6 +57,7 @@
@interface MPExpressionView (MPSelectionHelper)
- (void)restartCaretTimer;
- (void)updateCaret:(NSTimer *)timer;
@@ -101,6 +101,7 @@
[self updateCaret:self.caretTimer];
}
- (void)updateCaret:(NSTimer *)timer
{
self.caretVisible = !self.caretVisible;
@@ -111,6 +112,7 @@
[self setNeedsDisplayInRect:updatedRect];
}
- (NSRect)selectionRect
{
NSRect selectionRect = [self.expressionStorage.rootLayout boundingRectForRangePath:self.selection];
@@ -120,6 +122,7 @@
return selectionRect;
}
- (NSIndexPath *)selectionToTheRightOf:(NSIndexPath *)selectionPath
byExtendingSelection:(BOOL)extendingSelection
selectWords:(BOOL)selectWords
@@ -186,6 +189,7 @@
return [selectionPath indexPathByReplacingLastIndexWithIndex:locationInTarget];
}
- (NSIndexPath *)selectionToTheLeftOf:(NSIndexPath *)selectionPath
byExtendingSelection:(BOOL)extendingSelection
selectWords:(BOOL)selectWords
@@ -257,6 +261,7 @@
return [selectionPath indexPathByReplacingLastIndexWithIndex:locationInTarget];
}
- (MPRangePath *)rangePathEnclosingAnchorPath:(NSIndexPath *)anchorPath
newSelectionPath:(NSIndexPath *)newSelectionPath
{
@@ -312,9 +317,13 @@
#pragma mark -
@implementation MPExpressionView
#pragma mark Creation Methods
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
@@ -324,6 +333,7 @@
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
@@ -333,6 +343,7 @@
return self;
}
- (void)initializeExpressionView
{
// Setup the Expression Storage
@@ -348,6 +359,7 @@
[self restartCaretTimer];
}
- (void)initializeButtons
{
NSButton *functionsButton = self.functionsButton;
@@ -368,6 +380,7 @@
constant:0]];
}
- (void)initializeErrorsViews
{
@@ -394,34 +407,42 @@
views:variableBindings]];
}
#pragma mark - NSView Configuration
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (BOOL)canBecomeKeyView
{
return YES;
}
- (BOOL)isOpaque
{
return YES;
}
- (void)resetCursorRects
{
[self addCursorRect:self.bounds
cursor:[NSCursor IBeamCursor]];
}
- (BOOL)becomeFirstResponder
{
[self restartCaretTimer];
return [super becomeFirstResponder];
}
- (BOOL)resignFirstResponder
{
[self.caretTimer invalidate];
@@ -430,12 +451,14 @@
return [super resignFirstResponder];
}
- (void)setFrame:(NSRect)frameRect
{
[self setNeedsLayout:YES];
[super setFrame:frameRect];
}
- (NSSize)intrinsicContentSize
{
NSSize size = self.expressionStorage.rootLayout.bounds.size;
@@ -443,7 +466,10 @@
return size;
}
#pragma mark - Properties
- (void)setExpressionStorage:(MPExpressionStorage *)expressionStorage
{
_expressionStorage.expressionView = nil;
@@ -452,6 +478,7 @@
[self invalidateIntrinsicContentSize];
}
- (void)setSelection:(MPRangePath *)selection
{
_selection = selection;
@@ -459,6 +486,7 @@
self.needsDisplay = YES;
}
- (void)setSyntaxErrors:(NSArray *)syntaxErrors
{
_syntaxErrors = syntaxErrors;
@@ -491,12 +519,14 @@
}
}
- (void)setMathError:(NSError *)mathError
{
_mathError = mathError;
self.mathErrorTextField.stringValue = mathError != nil ? mathError.localizedDescription : @"";
}
- (NSButton *)functionsButton
{
if (!_functionsButton) {
@@ -520,6 +550,7 @@
return _functionsButton;
}
- (NSPopUpButton *)syntaxErrorsPopUpButton
{
if (!_syntaxErrorsPopUpButton) {
@@ -539,6 +570,7 @@
return _syntaxErrorsPopUpButton;
}
- (NSTextField *)mathErrorTextField
{
if (!_mathErrorTextField) {
@@ -555,6 +587,7 @@
return _mathErrorTextField;
}
- (void)didSelectError:(NSPopUpButton *)sender
{
NSError *error = self.syntaxErrors[sender.indexOfSelectedItem-1];
@@ -564,7 +597,10 @@
self.selection = MPMakeRangePath(pathToExpression, errorRange.length);
}
#pragma mark - Mouse Event Handling
- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint pointInView = [self convertPoint:theEvent.locationInWindow
@@ -577,6 +613,7 @@
self.selection = MPMakeRangePath(selectionPath, 0);
}
- (void)mouseDragged:(NSEvent *)theEvent
{
NSPoint pointInView = [self convertPoint:theEvent.locationInWindow
@@ -590,7 +627,10 @@
newSelectionPath:mouseSelectionPath];
}
#pragma mark Key Event Handling
- (void)keyDown:(NSEvent *)theEvent
{
NSString *characters = theEvent.characters;
@@ -617,6 +657,15 @@
[allowedCharacters addCharactersInString:[NSString stringWithFormat:@"+-*= !%@", decimalSeparator]];
if (characters.length == 1 && [characters stringByTrimmingCharactersInSet:allowedCharacters].length == 0) {
if ([characters isEqualTo:@"i"]) {
if (self.selection.location.lastIndex > 0) {
id<MPExpressionElement> symbol = [self.expressionStorage symbolAtIndex:self.selection.location.lastIndex-1];
if ([symbol isEqual:@"p"]) {
self.selection = MPMakeRangePath([self.selection.location indexPathByDecrementingLastIndex], self.selection.length+1);
characters = @"π";
}
}
}
if ([characters isEqualToString:@"*"]) {
characters = @"⋅";
}
@@ -629,22 +678,28 @@
}
}
#pragma mark - Actions
- (void)switchToDegrees:(id)sender
{
[MPMathRules sharedRules].isUsingDegrees = YES;
}
- (void)switchToRadians:(id)sender
{
[MPMathRules sharedRules].isUsingDegrees = NO;
}
- (void)switchRadiansDegrees:(id)sender
{
[MPMathRules sharedRules].isUsingDegrees = ![MPMathRules sharedRules].isUsingDegrees;
}
- (IBAction)toggleFunctionsPopover:(id)sender
{
if (self.functionsPopover == nil || self.functionsViewController == nil) {
@@ -663,6 +718,7 @@
}
}
- (void)insertFunction:(MPFunction *)function
{
[self.functionsPopover close];
@@ -678,8 +734,10 @@
self.selection = MPMakeRangePath([[functionPath indexPathByAddingIndex:functionLayout.indexOfLeadingChild] indexPathByAddingIndex:0], 0);
}
#pragma mark Editing Actions
- (void)insertParenthesisFunction:(id)sender
{
MPExpression *selectedElementsExpression = [self.expressionStorage subexpressionWithRangePath:self.selection
@@ -698,6 +756,7 @@
self.selection = MPMakeRangePath([[functionPath indexPathByAddingIndex:0] indexPathByAddingIndex:0], self.selection.length);
}
- (void)insertClosingParenthesis
{
if (self.selection.length > 0) {
@@ -721,6 +780,7 @@
}
}
- (void)insertPowerFunction
{
MPExpression *selectedElementsExpression = [self.expressionStorage subexpressionWithRangePath:self.selection
@@ -739,6 +799,7 @@
self.selection = MPMakeRangePath([[functionPath indexPathByAddingIndex:0] indexPathByAddingIndex:0], self.selection.length);
}
- (void)insertFractionFunction
{
if (self.selection.length == 0) {
@@ -792,6 +853,7 @@
}
}
- (void)insertNewline:(id)sender
{
if (self.target && self.action) {
@@ -801,6 +863,7 @@
}
}
- (void)deleteBackward:(id)sender
{
if (self.selection.length > 0) {
@@ -853,6 +916,7 @@
}
}
- (void)delete:(id)sender
{
[self.expressionStorage replaceItemsInRangePath:self.selection
@@ -861,8 +925,10 @@
self.selection = MPMakeRangePath(self.selection.location, 0);
}
#pragma mark Selection Actions
- (void)moveRight:(id)sender
{
if (self.selection.length > 0) {
@@ -875,6 +941,7 @@
}
}
- (void)moveLeft:(id)sender
{
if (self.selection.length > 0) {
@@ -887,6 +954,7 @@
}
}
- (void)moveWordRight:(id)sender
{
NSIndexPath *location = self.selection.maxRangePath;
@@ -896,6 +964,7 @@
self.selection = MPMakeRangePath(newSelectionLocation, 0);
}
- (void)moveWordLeft:(id)sender
{
NSIndexPath *location = self.selection.location;
@@ -905,17 +974,20 @@
self.selection = MPMakeRangePath(newSelectionLocation, 0);
}
- (void)moveToBeginningOfLine:(id)sender
{
self.selection = MPMakeRangePath([self.selection.location indexPathByReplacingLastIndexWithIndex:0], 0);
}
- (void)moveToEndOfLine:(id)sender
{
MPExpression *targetExpression = [self.expressionStorage elementAtIndexPath:[self.selection.location indexPathByRemovingLastIndex]];
self.selection = MPMakeRangePath([self.selection.location indexPathByReplacingLastIndexWithIndex:targetExpression.countSymbols], 0);
}
- (void)moveLeftAndModifySelection:(id)sender
{
if (self.selection.length == 0) {
@@ -935,6 +1007,7 @@
self.selection = [self rangePathEnclosingAnchorPath:maxLocation newSelectionPath:location];
}
- (void)moveRightAndModifySelection:(id)sender
{
if (self.selection.length == 0) {
@@ -955,6 +1028,7 @@
self.selection = MPMakeRangePath(location, maxLocation.lastIndex-location.lastIndex);
}
- (void)moveWordRightAndModifySelection:(id)sender
{
if (self.selection.length == 0) {
@@ -977,6 +1051,7 @@
self.selection = MPMakeRangePath(location, maxLocation.lastIndex-location.lastIndex);
}
- (void)moveWordLeftAndModifySelection:(id)sender
{
if (self.selection.length == 0) {
@@ -999,6 +1074,7 @@
self.selection = MPMakeRangePath(location, maxLocation.lastIndex-location.lastIndex);
}
- (void)moveUp:(id)sender
{
NSIndexPath *targetExpressionPath = [self.selection.location indexPathByRemovingLastIndex];
@@ -1011,6 +1087,7 @@
}
}
- (void)moveDown:(id)sender
{
NSIndexPath *targetExpressionPath = [self.selection.location indexPathByRemovingLastIndex];
@@ -1023,13 +1100,17 @@
}
}
- (void)selectAll:(id)sender
{
NSIndexPath *location = [NSIndexPath indexPathWithIndex:0];
self.selection = MPMakeRangePath(location, self.expressionStorage.countSymbols);
}
#pragma mark Drawing Methods
- (void)drawRect:(NSRect)dirtyRect
{
// Draw the background
@@ -1066,6 +1147,7 @@
#pragma mark - User Interface Validations
- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)anItem
{
BOOL degrees = [MPMathRules sharedRules].isUsingDegrees;