failed attempts to intercept copy command when the cursor is in the empty text field...
[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         if (![self fileName]) {
38                 [arrayController add:self];
39         }
40         [arrayController setSelectionIndex:[calculations count] - 1];
41 }
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         id lastObject = [calculations lastObject];
60         id selectedObject = [[arrayController selectedObjects] objectAtIndex:0];
61         return selectedObject == lastObject;
62 }
63
64
65 - (void)addItem {
66         Calculation *newcalc = [[[Calculation alloc] init] autorelease];
67         [arrayController addObject:newcalc];
68         int count = [calculations count];
69         id secondtolast = [calculations objectAtIndex:count - 2];
70         [secondtolast setValue:newcalc forKey:@"next"];
71 }
72
73
74 - (void)insertPrevious:(NSNumber *)sender {
75         int index = [sender intValue];
76         if ([calculations count] <= index) return;
77         
78         NSString *value = [[calculations objectAtIndex:[calculations count] - index - 1] valueForKey:@"result"];
79         [[expressionField currentEditor] insertText:value];
80 }
81
82
83 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int *)index {
84
85         NSString *partialWord = [[textView string] substringWithRange:charRange];
86         NSArray *completions = [[[NSApplication sharedApplication] delegate] completionsForPartialWord:partialWord];
87
88         return completions;
89 }
90
91
92 // enable copy: menu command only when a previous result is selected
93 - (BOOL)validateMenuItem:(NSMenuItem *)item {
94         if ([item action] != @selector(copy:)) return YES;
95         id selectedObject = [[arrayController selectedObjects] objectAtIndex:0];
96         return selectedObject && ![self lastItemIsSelected] && ![selectedObject expressionIsEmpty];
97 }
98
99 /*
100 - (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)aSelector {
101         NSLog(@"text view by selector %d", aSelector);
102         return NO;
103 }
104 */
105
106
107 - (IBAction)copy:(id)sender {
108         NSPasteboard *pboard = [NSPasteboard generalPasteboard];
109         [pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
110         id selectedObject = [[arrayController selectedObjects] objectAtIndex:0];
111         [pboard setString:[selectedObject result] forType:NSStringPboardType];
112 }
113
114
115 - (NSString *)windowNibName {
116     return @"MyDocument";
117 }
118
119
120 - (void)windowControllerDidLoadNib:(NSWindowController *)aController {
121         NSWindow *win = [aController window];
122         [win performSelector:@selector(makeFirstResponder:) withObject:expressionField afterDelay:0.0];
123     [super windowControllerDidLoadNib:aController];
124 }
125
126
127 - (NSData *)dataRepresentationOfType:(NSString *)aType {
128         NSMutableData *data = [[[NSMutableData alloc] init] autorelease];
129         NSKeyedArchiver *archiver = [[[NSKeyedArchiver alloc] initForWritingWithMutableData:data] autorelease];
130         [archiver setOutputFormat:NSPropertyListXMLFormat_v1_0];
131         [archiver encodeObject:calculations forKey:@"calculations"];
132         [archiver finishEncoding];
133         return data;
134 }
135
136
137 - (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType {
138         NSKeyedUnarchiver *unarchiver = [[[NSKeyedUnarchiver alloc] initForReadingWithData:data] autorelease];
139         [self setValue:[unarchiver decodeObjectForKey:@"calculations"] forKey:@"calculations"];
140         [unarchiver finishDecoding];
141     return YES;
142 }
143
144 @end