5 // Created by Marc Liyanage on 31.01.05.
6 // Copyright __MyCompanyName__ 2005 . All rights reserved.
12 - num lock / esc clear expression
18 #import "MyDocument.h"
20 @implementation MyDocument
24 if (!self) return nil;
25 calculations = [[NSMutableArray alloc] init];
31 [calculations release];
35 - (void)awakeFromNib {
37 if (![self fileName]) {
38 [arrayController add:self];
41 [arrayController setSelectionIndex:[calculations count] - 1];
45 - (IBAction)commit:(id)sender {
47 if (![[sender stringValue] length]) return;
49 if (![self lastItemIsSelected] && [[calculations lastObject] expressionIsEmpty]) {
50 [arrayController setSelectedObjects:[NSArray arrayWithObject:[calculations lastObject]]];
58 - (BOOL)lastItemIsSelected {
60 id lastObject = [calculations lastObject];
61 id selectedObject = [[arrayController selectedObjects] objectAtIndex:0];
63 return selectedObject == lastObject;
69 [arrayController add:self];
70 int count = [calculations count];
71 id last = [calculations objectAtIndex:count - 1];
72 id secondtolast = [calculations objectAtIndex:count - 2];
73 [secondtolast setValue:last forKey:@"next"];
78 - (void)insertPrevious:(NSNumber *)sender {
80 int index = [sender intValue];
81 if ([calculations count] <= index) return;
83 NSString *value = [[calculations objectAtIndex:[calculations count] - index - 1] valueForKey:@"result"];
84 [[expressionField currentEditor] insertText:value];
88 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int *)index {
90 NSString *partialWord = [[textView string] substringWithRange:charRange];
91 NSArray *completions = [[[NSApplication sharedApplication] delegate] completionsForPartialWord:partialWord];
98 - (NSString *)windowNibName {
103 - (void)windowControllerDidLoadNib:(NSWindowController *) aController {
104 [super windowControllerDidLoadNib:aController];
108 - (NSData *)dataRepresentationOfType:(NSString *)aType {
109 NSMutableData *data = [[[NSMutableData alloc] init] autorelease];
110 NSKeyedArchiver *archiver = [[[NSKeyedArchiver alloc] initForWritingWithMutableData:data] autorelease];
111 [archiver setOutputFormat:NSPropertyListXMLFormat_v1_0];
112 [archiver encodeObject:calculations forKey:@"calculations"];
113 [archiver finishEncoding];
118 - (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType {
119 NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:data] autorelease];
120 [self setValue:[unarchiver decodeObjectForKey:@"calculations"] forKey:@"calculations"];
121 [unarchiver finishDecoding];