Initial revision
[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
19 #import "MyDocument.h"
20
21 @implementation MyDocument
22
23 - (id)init {
24
25     self = [super init];
26     if (!self) return nil;
27
28         calculations = [[NSMutableArray alloc] init];
29         
30     return self;
31 }
32
33
34
35 - (void)dealloc {
36         [calculations release];
37 }
38
39
40
41 - (void)awakeFromNib {
42
43         if (![self fileName]) {
44                 [arrayController add:self];
45         }
46         
47         [arrayController setSelectionIndex:[calculations count] - 1];
48
49 }
50
51
52 - (IBAction)commit:(id)sender {
53
54         if (![[sender stringValue] length]) return;
55
56         if (![self lastItemIsSelected] && [[calculations lastObject] expressionIsEmpty]) {
57                 [arrayController setSelectedObjects:[NSArray arrayWithObject:[calculations lastObject]]];
58                 return;
59         }
60         [self addItem];
61 }
62
63
64
65 - (BOOL)lastItemIsSelected {
66         
67         id lastObject = [calculations lastObject];
68         id selectedObject = [[arrayController selectedObjects] objectAtIndex:0];
69
70         return selectedObject == lastObject;
71         
72 }
73
74
75 - (void)addItem {
76
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"];
82         
83 }
84
85
86 - (void)insertPrevious:(NSNumber *)sender {
87
88         int index = [sender intValue];
89         if ([calculations count] <= index) return;
90         
91         NSString *value = [[calculations objectAtIndex:[calculations count] - index - 1] valueForKey:@"result"];
92         [[expressionField currentEditor] insertText:value];
93 }
94
95
96
97
98
99 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int *)index {
100
101         NSString *partialWord = [[textView string] substringWithRange:charRange];
102         NSArray *completions = [[[NSApplication sharedApplication] delegate] completionsForPartialWord:partialWord];
103
104         return completions;
105         
106 }
107
108
109
110 - (NSString *)windowNibName
111 {
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";
115 }
116
117 - (void)windowControllerDidLoadNib:(NSWindowController *) aController
118 {
119     [super windowControllerDidLoadNib:aController];
120     // Add any code here that needs to be executed once the windowController has loaded the document's window.
121
122 }
123
124 - (NSData *)dataRepresentationOfType:(NSString *)aType {
125
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];
131         return data;
132 }
133
134
135 - (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType {
136
137         NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:data] autorelease];
138         [self setValue:[unarchiver decodeObjectForKey:@"calculations"] forKey:@"calculations"];
139         [unarchiver finishDecoding];
140
141     return YES;
142 }
143
144 @end