5 // Created by Marc Liyanage on 31.01.05.
6 // Copyright __MyCompanyName__ 2005 . All rights reserved.
12 - num lock / esc clear expression
19 #import "MyDocument.h"
21 @implementation MyDocument
26 if (!self) return nil;
28 calculations = [[NSMutableArray alloc] init];
36 [calculations release];
41 - (void)awakeFromNib {
43 if (![self fileName]) {
44 [arrayController add:self];
47 [arrayController setSelectionIndex:[calculations count] - 1];
52 - (IBAction)commit:(id)sender {
54 if (![[sender stringValue] length]) return;
56 if (![self lastItemIsSelected] && [[calculations lastObject] expressionIsEmpty]) {
57 [arrayController setSelectedObjects:[NSArray arrayWithObject:[calculations lastObject]]];
65 - (BOOL)lastItemIsSelected {
67 id lastObject = [calculations lastObject];
68 id selectedObject = [[arrayController selectedObjects] objectAtIndex:0];
70 return selectedObject == lastObject;
77 [arrayController add:self];
78 int count = [calculations count];
79 id last = [calculations objectAtIndex:count - 1];
80 id secondtolast = [calculations objectAtIndex:count - 2];
81 [secondtolast setValue:last forKey:@"next"];
86 - (void)insertPrevious:(NSNumber *)sender {
88 int index = [sender intValue];
89 if ([calculations count] <= index) return;
91 NSString *value = [[calculations objectAtIndex:[calculations count] - index - 1] valueForKey:@"result"];
92 [[expressionField currentEditor] insertText:value];
99 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int *)index {
101 NSString *partialWord = [[textView string] substringWithRange:charRange];
102 NSArray *completions = [[[NSApplication sharedApplication] delegate] completionsForPartialWord:partialWord];
110 - (NSString *)windowNibName
112 // Override returning the nib file name of the document
113 // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
114 return @"MyDocument";
117 - (void)windowControllerDidLoadNib:(NSWindowController *) aController
119 [super windowControllerDidLoadNib:aController];
120 // Add any code here that needs to be executed once the windowController has loaded the document's window.
124 - (NSData *)dataRepresentationOfType:(NSString *)aType {
126 NSMutableData *data = [[[NSMutableData alloc] init] autorelease];
127 NSKeyedArchiver *archiver = [[[NSKeyedArchiver alloc] initForWritingWithMutableData:data] autorelease];
128 [archiver setOutputFormat:NSPropertyListXMLFormat_v1_0];
129 [archiver encodeObject:calculations forKey:@"calculations"];
130 [archiver finishEncoding];
135 - (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType {
137 NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:data] autorelease];
138 [self setValue:[unarchiver decodeObjectForKey:@"calculations"] forKey:@"calculations"];
139 [unarchiver finishDecoding];