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];
36 - (void)awakeFromNib {
37 if (![self fileName]) {
38 [arrayController add:self];
40 [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 {
59 id lastObject = [calculations lastObject];
60 id selectedObject = [[arrayController selectedObjects] objectAtIndex:0];
61 return selectedObject == lastObject;
66 Calculation *newcalc = [[[Calculation alloc] init] autorelease];
67 [arrayController addObject:newcalc];
68 int count = [calculations count];
69 id secondtolast = [calculations objectAtIndex:count - 2];
70 [secondtolast setValue:newcalc forKey:@"next"];
74 - (void)insertPrevious:(NSNumber *)sender {
75 int index = [sender intValue];
76 if ([calculations count] <= index) return;
78 NSString *value = [[calculations objectAtIndex:[calculations count] - index - 1] valueForKey:@"result"];
79 [[expressionField currentEditor] insertText:value];
83 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int *)index {
85 NSString *partialWord = [[textView string] substringWithRange:charRange];
86 NSArray *completions = [[[NSApplication sharedApplication] delegate] completionsForPartialWord:partialWord];
92 // enable copy: menu command only when a previous result is selected
93 - (BOOL)validateMenuItem:(NSMenuItem *)item {
94 if ([item action] != @selector(copy:)) return YES;
95 id selectedObject = [[arrayController selectedObjects] objectAtIndex:0];
96 return selectedObject && ![self lastItemIsSelected] && ![selectedObject expressionIsEmpty];
100 - (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)aSelector {
101 NSLog(@"text view by selector %d", aSelector);
107 - (IBAction)copy:(id)sender {
108 NSPasteboard *pboard = [NSPasteboard generalPasteboard];
109 [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
110 id selectedObject = [[arrayController selectedObjects] objectAtIndex:0];
111 [pboard setString:[selectedObject result] forType:NSStringPboardType];
115 - (NSString *)windowNibName {
116 return @"MyDocument";
120 - (void)windowControllerDidLoadNib:(NSWindowController *)aController {
121 NSWindow *win = [aController window];
122 [win performSelector:@selector(makeFirstResponder:) withObject:expressionField afterDelay:0.0];
123 [super windowControllerDidLoadNib:aController];
127 - (NSData *)dataRepresentationOfType:(NSString *)aType {
128 NSMutableData *data = [[[NSMutableData alloc] init] autorelease];
129 NSKeyedArchiver *archiver = [[[NSKeyedArchiver alloc] initForWritingWithMutableData:data] autorelease];
130 [archiver setOutputFormat:NSPropertyListXMLFormat_v1_0];
131 [archiver encodeObject:calculations forKey:@"calculations"];
132 [archiver finishEncoding];
137 - (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType {
138 NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:data] autorelease];
139 [self setValue:[unarchiver decodeObjectForKey:@"calculations"] forKey:@"calculations"];
140 [unarchiver finishDecoding];