cleanup, reformatting
[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 }
33
34
35 - (void)awakeFromNib {
36
37         if (![self fileName]) {
38                 [arrayController add:self];
39         }
40         
41         [arrayController setSelectionIndex:[calculations count] - 1];
42 }
43
44
45 - (IBAction)commit:(id)sender {
46
47         if (![[sender stringValue] length]) return;
48
49         if (![self lastItemIsSelected] && [[calculations lastObject] expressionIsEmpty]) {
50                 [arrayController setSelectedObjects:[NSArray arrayWithObject:[calculations lastObject]]];
51                 return;
52         }
53         [self addItem];
54 }
55
56
57
58 - (BOOL)lastItemIsSelected {
59         
60         id lastObject = [calculations lastObject];
61         id selectedObject = [[arrayController selectedObjects] objectAtIndex:0];
62
63         return selectedObject == lastObject;
64 }
65
66
67 - (void)addItem {
68
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"];
74         
75 }
76
77
78 - (void)insertPrevious:(NSNumber *)sender {
79
80         int index = [sender intValue];
81         if ([calculations count] <= index) return;
82         
83         NSString *value = [[calculations objectAtIndex:[calculations count] - index - 1] valueForKey:@"result"];
84         [[expressionField currentEditor] insertText:value];
85 }
86
87
88 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int *)index {
89
90         NSString *partialWord = [[textView string] substringWithRange:charRange];
91         NSArray *completions = [[[NSApplication sharedApplication] delegate] completionsForPartialWord:partialWord];
92
93         return completions;
94 }
95
96
97
98 - (NSString *)windowNibName {
99     return @"MyDocument";
100 }
101
102
103 - (void)windowControllerDidLoadNib:(NSWindowController *) aController {
104     [super windowControllerDidLoadNib:aController];
105 }
106
107
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];
114         return data;
115 }
116
117
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];
122     return YES;
123 }
124
125 @end