Debian Codegraph @

cocoa/app/AiksaurusCocoaMain.m

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2
3 /* Aiksaurus / Cocoa front-end
4 * Copyright (C) 2003 Francis James Franklin <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22 #import "AiksaurusCocoaMain.h"
23
24 @implementation AiksaurusCocoaMain
25
26 - (void)awakeFromNib
27 {
28 NSSize size;
29
30 m_aiksaurus = [[AiksaurusCocoa alloc] init];
31
32 if (m_aiksaurus)
33 {
34 if ([m_aiksaurus okay])
35 {
36 [oStatus setTextColor:[NSColor blackColor]];
37 [oStatus setStringValue:@"Yay! Give me a word, please..."];
38
39 [oResultsTable setDataSource:m_aiksaurus];
40 [oResultsTable setDelegate:m_aiksaurus];
41 }
42 else
43 {
44 [oStatus setTextColor:[NSColor redColor]];
45 [oStatus setStringValue:[m_aiksaurus lastError]];
46 }
47 }
48 else
49 {
50 [oStatus setTextColor:[NSColor redColor]];
51 [oStatus setStringValue:@"Error: Insufficient memory!"];
52 }
53 if ([oHistory numberOfItems])
54 [oHistory removeAllItems];
55
56 size.width = 0;
57 size.height = 1;
58
59 [oResultsTable setIntercellSpacing:size];
60 [oResultsTable setDoubleAction:@selector(aDoubleClick:)];
61
62 [NSApp setServicesProvider:self];
63
64 [self sync];
65 }
66
67 - (void)dealloc
68 {
69 if (m_aiksaurus)
70 {
71 [m_aiksaurus dealloc];
72 m_aiksaurus = nil;
73 }
74 [super dealloc];
75 }
76
77 - (void)pasteViaService:(NSPasteboard *)pboard userData:(NSString *)userData error:(NSString **)error
78 {
79 NSString * word = nil;
80
81 NSArray * types = [pboard types];
82
83 if ([types containsObject:NSStringPboardType])
84 {
85 word = [pboard stringForType:NSStringPboardType];
86 if (word)
87 {
88 [oSearchField setStringValue:word];
89 [self aSearchField:self];
90 }
91 }
92 }
93
94 - (void)sync
95 {
96 if ([m_aiksaurus historyCanGoBack])
97 [oBack setEnabled:YES];
98 else
99 [oBack setEnabled:NO];
100
101 if ([m_aiksaurus historyCanGoForward])
102 [oForward setEnabled:YES];
103 else
104 [oForward setEnabled:NO];
105
106 if ([m_aiksaurus word])
107 [oHistory setEnabled:YES];
108 else
109 [oHistory setEnabled:NO];
110
111 [oResultsTable reloadData];
112 }
113
114 - (IBAction)aBack:(id)sender
115 {
116 if ([m_aiksaurus historyBack])
117 {
118 [oHistory selectItemWithTitle:[m_aiksaurus word]];
119 [self sync];
120 }
121 }
122
123 - (IBAction)aForward:(id)sender
124 {
125 if ([m_aiksaurus historyForward])
126 {
127 [oHistory selectItemWithTitle:[m_aiksaurus word]];
128 [self sync];
129 }
130 }
131
132 - (IBAction)aHistory:(id)sender
133 {
134 NSString * old_word = [m_aiksaurus word];
135 NSString * new_word = [oHistory titleOfSelectedItem];
136
137 if (old_word == nil)
138 {
139 if ([new_word length])
140 {
141 [m_aiksaurus lookupWord:new_word reorderHistory:NO];
142 [self sync];
143 }
144 }
145 else if ([new_word length])
146 {
147 if ([new_word isEqualToString:old_word] == NO)
148 {
149 [m_aiksaurus lookupWord:new_word reorderHistory:NO];
150 [self sync];
151 }
152 }
153 }
154
155 - (IBAction)aDoubleClick:(id)sender
156 {
157 NSString * value = [m_aiksaurus clickColumn:[oResultsTable clickedColumn] atRow:[oResultsTable clickedRow]];
158
159 if (value)
160 {
161 [oSearchField setStringValue:value];
162 [self aSearchField:sender];
163 }
164 }
165
166 - (IBAction)aResultsTable:(id)sender
167 {
168 NSString * value = [m_aiksaurus clickColumn:[oResultsTable clickedColumn] atRow:[oResultsTable clickedRow]];
169
170 if (value)
171 {
172 [oSearchField setStringValue:value];
173 [oResultsTable reloadData];
174 }
175 [oStatus setStringValue:@""];
176 }
177
178 - (IBAction)aSearch:(id)sender
179 {
180 [self aSearchField:sender];
181 }
182
183 - (IBAction)aSearchField:(id)sender
184 {
185 NSString * word = [oSearchField stringValue];
186 #if 0
187 static AiksaurusCocoaPanel * panel = nil;
188 if (panel == nil)
189 {
190 panel = (AiksaurusCocoaPanel *) [[AiksaurusCocoaPanel alloc] init];
191 }
192 if (panel)
193 {
194 [panel setTargetForInsert:self withAction:@selector(insertWord:)];
195 [panel showWindow:self];
196 }
197 #endif
198 if (word)
199 {
200 if ([m_aiksaurus lookupWord:word reorderHistory:YES])
201 {
202 [oHistory insertItemWithTitle:word atIndex:0];
203 [oHistory selectItemAtIndex:0];
204 [self sync];
205 [oStatus setTextColor:[NSColor blackColor]];
206 [oStatus setStringValue:@"Next please..."];
207 }
208 else if ([m_aiksaurus okay])
209 {
210 [self sync];
211 [oStatus setTextColor:[NSColor blueColor]];
212 [oStatus setStringValue:@"No synonyms known. Nearby words:"];
213 }
214 else
215 {
216 [oStatus setTextColor:[NSColor redColor]];
217 [oStatus setStringValue:[m_aiksaurus lastError]];
218 }
219 }
220 }
221
222 - (void)insertWord:(NSString *)word
223 {
224 if (word)
225 {
226 [oSearchField setStringValue:word];
227 [self aSearchField:self];
228 }
229 }
230
231 @end