focus expression text field at startup -> version 1.2
[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
47
48 - (IBAction)commit:(id)sender {
49
50         if (![[sender stringValue] length]) return;
51
52         if (![self lastItemIsSelected] && [[calculations lastObject] expressionIsEmpty]) {
53                 [arrayController setSelectedObjects:[NSArray arrayWithObject:[calculations lastObject]]];
54                 return;
55         }
56         [self addItem];
57 }
58
59
60
61 - (BOOL)lastItemIsSelected {
62         
63         id lastObject = [calculations lastObject];
64         id selectedObject = [[arrayController selectedObjects] objectAtIndex:0];
65
66         return selectedObject == lastObject;
67 }
68
69
70 - (void)addItem {
71
72         Calculation *newcalc = [[[Calculation alloc] init] autorelease];
73         [arrayController addObject:newcalc];
74         int count = [calculations count];
75         id secondtolast = [calculations objectAtIndex:count - 2];
76         [secondtolast setValue:newcalc forKey:@"next"];
77         
78 }
79
80
81 - (void)insertPrevious:(NSNumber *)sender {
82
83         int index = [sender intValue];
84         if ([calculations count] <= index) return;
85         
86         NSString *value = [[calculations objectAtIndex:[calculations count] - index - 1] valueForKey:@"result"];
87         [[expressionField currentEditor] insertText:value];
88 }
89
90
91 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int *)index {
92
93         NSString *partialWord = [[textView string] substringWithRange:charRange];
94         NSArray *completions = [[[NSApplication sharedApplication] delegate] completionsForPartialWord:partialWord];
95
96         return completions;
97 }
98
99
100
101 - (NSString *)windowNibName {
102     return @"MyDocument";
103 }
104
105
106 - (void)windowControllerDidLoadNib:(NSWindowController *)aController {
107         NSWindow *win = [aController window];
108         [win performSelector:@selector(makeFirstResponder:) withObject:expressionField afterDelay:0.0];
109     [super windowControllerDidLoadNib:aController];
110 }
111
112
113 - (NSData *)dataRepresentationOfType:(NSString *)aType {
114         NSMutableData *data = [[[NSMutableData alloc] init] autorelease];
115         NSKeyedArchiver *archiver = [[[NSKeyedArchiver alloc] initForWritingWithMutableData:data] autorelease];
116         [archiver setOutputFormat:NSPropertyListXMLFormat_v1_0];
117         [archiver encodeObject:calculations forKey:@"calculations"];
118         [archiver finishEncoding];
119         return data;
120 }
121
122
123 - (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType {
124         NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:data] autorelease];
125         [self setValue:[unarchiver decodeObjectForKey:@"calculations"] forKey:@"calculations"];
126         [unarchiver finishDecoding];
127     return YES;
128 }
129
130 @end