// // MyDocument.m // LeanCalc // // Created by Marc Liyanage on 31.01.05. // Copyright __MyCompanyName__ 2005 . All rights reserved. // /* todo - num lock / esc clear expression */ #import "MyDocument.h" @implementation MyDocument - (id)init { self = [super init]; if (!self) return nil; calculations = [[NSMutableArray alloc] init]; return self; } - (void)dealloc { [calculations release]; [super dealloc]; } - (void)awakeFromNib { if (![self fileName]) { [arrayController add:self]; } [arrayController setSelectionIndex:[calculations count] - 1]; } - (IBAction)commit:(id)sender { if (![[sender stringValue] length]) return; if (![self lastItemIsSelected] && [[calculations lastObject] expressionIsEmpty]) { [arrayController setSelectedObjects:[NSArray arrayWithObject:[calculations lastObject]]]; return; } [self addItem]; } - (BOOL)lastItemIsSelected { id lastObject = [calculations lastObject]; id selectedObject = [[arrayController selectedObjects] objectAtIndex:0]; return selectedObject == lastObject; } - (void)addItem { Calculation *newcalc = [[[Calculation alloc] init] autorelease]; [arrayController addObject:newcalc]; int count = [calculations count]; id secondtolast = [calculations objectAtIndex:count - 2]; [secondtolast setValue:newcalc forKey:@"next"]; } - (void)insertPrevious:(NSNumber *)sender { int index = [sender intValue]; if ([calculations count] <= index) return; NSString *value = [[calculations objectAtIndex:[calculations count] - index - 1] valueForKey:@"result"]; [[expressionField currentEditor] insertText:value]; } - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int *)index { NSString *partialWord = [[textView string] substringWithRange:charRange]; NSArray *completions = [[[NSApplication sharedApplication] delegate] completionsForPartialWord:partialWord]; return completions; } // enable copy: menu command only when a previous result is selected - (BOOL)validateMenuItem:(NSMenuItem *)item { if ([item action] != @selector(copy:)) return YES; id selectedObject = [[arrayController selectedObjects] objectAtIndex:0]; return selectedObject && ![self lastItemIsSelected] && ![selectedObject expressionIsEmpty]; } /* - (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)aSelector { NSLog(@"text view by selector %d", aSelector); return NO; } */ - (IBAction)copy:(id)sender { NSPasteboard *pboard = [NSPasteboard generalPasteboard]; [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; id selectedObject = [[arrayController selectedObjects] objectAtIndex:0]; [pboard setString:[selectedObject result] forType:NSStringPboardType]; } - (NSString *)windowNibName { return @"MyDocument"; } - (void)windowControllerDidLoadNib:(NSWindowController *)aController { NSWindow *win = [aController window]; [win performSelector:@selector(makeFirstResponder:) withObject:expressionField afterDelay:0.0]; [super windowControllerDidLoadNib:aController]; } - (NSData *)dataRepresentationOfType:(NSString *)aType { NSMutableData *data = [[[NSMutableData alloc] init] autorelease]; NSKeyedArchiver *archiver = [[[NSKeyedArchiver alloc] initForWritingWithMutableData:data] autorelease]; [archiver setOutputFormat:NSPropertyListXMLFormat_v1_0]; [archiver encodeObject:calculations forKey:@"calculations"]; [archiver finishEncoding]; return data; } - (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType { NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:data] autorelease]; [self setValue:[unarchiver decodeObjectForKey:@"calculations"] forKey:@"calculations"]; [unarchiver finishDecoding]; return YES; } @end