added copy command to copy selected line result to the clipboard. bumped version...
[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         id lastObject = [calculations lastObject];
63         id selectedObject = [[arrayController selectedObjects] objectAtIndex:0];
64         return selectedObject == lastObject;
65 }
66
67
68 - (void)addItem {
69         Calculation *newcalc = [[[Calculation alloc] init] autorelease];
70         [arrayController addObject:newcalc];
71         int count = [calculations count];
72         id secondtolast = [calculations objectAtIndex:count - 2];
73         [secondtolast setValue:newcalc forKey:@"next"];
74 }
75
76
77 - (void)insertPrevious:(NSNumber *)sender {
78         int index = [sender intValue];
79         if ([calculations count] <= index) return;
80         
81         NSString *value = [[calculations objectAtIndex:[calculations count] - index - 1] valueForKey:@"result"];
82         [[expressionField currentEditor] insertText:value];
83 }
84
85
86 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int *)index {
87
88         NSString *partialWord = [[textView string] substringWithRange:charRange];
89         NSArray *completions = [[[NSApplication sharedApplication] delegate] completionsForPartialWord:partialWord];
90
91         return completions;
92 }
93
94
95 // enable copy: menu command only when a previous result is selected
96 - (BOOL)validateMenuItem:(NSMenuItem *)item {
97         if ([item action] != @selector(copy:)) return YES;
98         id selectedObject = [[arrayController selectedObjects] objectAtIndex:0];
99         return selectedObject && ![self lastItemIsSelected] && ![selectedObject expressionIsEmpty];
100 }
101
102
103 - (IBAction)copy:(id)sender {
104         NSPasteboard *pboard = [NSPasteboard generalPasteboard];
105         [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
106         id selectedObject = [[arrayController selectedObjects] objectAtIndex:0];
107         [pboard setString:[selectedObject result] forType:NSStringPboardType];
108 }
109
110
111
112
113 - (NSString *)windowNibName {
114     return @"MyDocument";
115 }
116
117
118 - (void)windowControllerDidLoadNib:(NSWindowController *)aController {
119         NSWindow *win = [aController window];
120         [win performSelector:@selector(makeFirstResponder:) withObject:expressionField afterDelay:0.0];
121     [super windowControllerDidLoadNib:aController];
122 }
123
124
125 - (NSData *)dataRepresentationOfType:(NSString *)aType {
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         NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:data] autorelease];
137         [self setValue:[unarchiver decodeObjectForKey:@"calculations"] forKey:@"calculations"];
138         [unarchiver finishDecoding];
139     return YES;
140 }
141
142 @end