version 1.1, universal binary, new xcode
[LeanCalc.git] / Sources / MyDocument.m
1 //
2 //  MyDocument.m
3 //  LeanCalc
4 //
5 //  Created by Marc Liyanage on 31.01.05.
6 //  Copyright __MyCompanyName__ 2005 . All rights reserved.
7 //
8
9
10 /* todo
11
12 - num lock / esc clear expression
13
14 */
15
16
17
18 #import "MyDocument.h"
19
20 @implementation MyDocument
21
22 - (id)init {
23     self = [super init];
24     if (!self) return nil;
25         calculations = [[NSMutableArray alloc] init];
26         return self;
27 }
28
29
30 - (void)dealloc {
31         [calculations release];
32         [super dealloc];
33 }
34
35
36 - (void)awakeFromNib {
37
38         if (![self fileName]) {
39                 [arrayController add:self];
40         }
41         
42         [arrayController setSelectionIndex:[calculations count] - 1];
43 }
44
45
46 - (IBAction)commit:(id)sender {
47
48         if (![[sender stringValue] length]) return;
49
50         if (![self lastItemIsSelected] && [[calculations lastObject] expressionIsEmpty]) {
51                 [arrayController setSelectedObjects:[NSArray arrayWithObject:[calculations lastObject]]];
52                 return;
53         }
54         [self addItem];
55 }
56
57
58
59 - (BOOL)lastItemIsSelected {
60         
61         id lastObject = [calculations lastObject];
62         id selectedObject = [[arrayController selectedObjects] objectAtIndex:0];
63
64         return selectedObject == lastObject;
65 }
66
67
68 - (void)addItem {
69
70         Calculation *newcalc = [[[Calculation alloc] init] autorelease];
71         [arrayController addObject:newcalc];
72         int count = [calculations count];
73         id secondtolast = [calculations objectAtIndex:count - 2];
74         [secondtolast setValue:newcalc forKey:@"next"];
75         
76 }
77
78
79 - (void)insertPrevious:(NSNumber *)sender {
80
81         int index = [sender intValue];
82         if ([calculations count] <= index) return;
83         
84         NSString *value = [[calculations objectAtIndex:[calculations count] - index - 1] valueForKey:@"result"];
85         [[expressionField currentEditor] insertText:value];
86 }
87
88
89 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int *)index {
90
91         NSString *partialWord = [[textView string] substringWithRange:charRange];
92         NSArray *completions = [[[NSApplication sharedApplication] delegate] completionsForPartialWord:partialWord];
93
94         return completions;
95 }
96
97
98
99 - (NSString *)windowNibName {
100     return @"MyDocument";
101 }
102
103
104 - (void)windowControllerDidLoadNib:(NSWindowController *) aController {
105     [super windowControllerDidLoadNib:aController];
106 }
107
108
109 - (NSData *)dataRepresentationOfType:(NSString *)aType {
110         NSMutableData *data = [[[NSMutableData alloc] init] autorelease];
111         NSKeyedArchiver *archiver = [[[NSKeyedArchiver alloc] initForWritingWithMutableData:data] autorelease];
112         [archiver setOutputFormat:NSPropertyListXMLFormat_v1_0];
113         [archiver encodeObject:calculations forKey:@"calculations"];
114         [archiver finishEncoding];
115         return data;
116 }
117
118
119 - (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType {
120         NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:data] autorelease];
121         [self setValue:[unarchiver decodeObjectForKey:@"calculations"] forKey:@"calculations"];
122         [unarchiver finishDecoding];
123     return YES;
124 }
125
126 @end