| 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 <objc/objc-runtime.h> |
| 23 |
#import <AppKit/NSNibLoading.h> |
| 24 |
|
| 25 |
#import "AiksaurusCocoa.h" |
| 26 |
|
| 27 |
@implementation AiksaurusCocoaPanel |
| 28 |
|
| 29 |
- (id)init |
| 30 |
{ |
| 31 |
NSBundle * bundle = nil; |
| 32 |
|
| 33 |
NSSize size; |
| 34 |
|
| 35 |
NSDictionary * dictionary = nil; |
| 36 |
|
| 37 |
[super initWithWindow:nil]; |
| 38 |
|
| 39 |
m_aiksaurus = [[AiksaurusCocoa alloc] init]; |
| 40 |
|
| 41 |
m_target = nil; |
| 42 |
|
| 43 |
bundle = [NSBundle bundleForClass:[self class]]; |
| 44 |
|
| 45 |
dictionary = [NSDictionary dictionaryWithObject:self forKey:@"NSOwner"]; |
| 46 |
|
| 47 |
if ([bundle loadNibFile:@"Panel" externalNameTable:dictionary withZone:[bundle zone]]) |
| 48 |
{ |
| 49 |
if (m_aiksaurus) |
| 50 |
{ |
| 51 |
if ([m_aiksaurus okay]) |
| 52 |
{ |
| 53 |
[oStatus setTextColor:[NSColor blackColor]]; |
| 54 |
[oStatus setStringValue:@"Yay! Give me a word, please..."]; |
| 55 |
|
| 56 |
[oResultsTable setDataSource:m_aiksaurus]; |
| 57 |
[oResultsTable setDelegate:m_aiksaurus]; |
| 58 |
} |
| 59 |
else |
| 60 |
{ |
| 61 |
[oStatus setTextColor:[NSColor redColor]]; |
| 62 |
[oStatus setStringValue:[m_aiksaurus lastError]]; |
| 63 |
} |
| 64 |
[self sync]; |
| 65 |
} |
| 66 |
else |
| 67 |
{ |
| 68 |
[oStatus setTextColor:[NSColor redColor]]; |
| 69 |
[oStatus setStringValue:@"Error: Insufficient memory!"]; |
| 70 |
} |
| 71 |
|
| 72 |
if ([oHistory numberOfItems]) |
| 73 |
[oHistory removeAllItems]; |
| 74 |
|
| 75 |
size.width = 0; |
| 76 |
size.height = 1; |
| 77 |
|
| 78 |
[oResultsTable setIntercellSpacing:size]; |
| 79 |
[oResultsTable setDoubleAction:@selector(aDoubleClick:)]; |
| 80 |
} |
| 81 |
return self; |
| 82 |
} |
| 83 |
|
| 84 |
- (void)dealloc |
| 85 |
{ |
| 86 |
if (m_aiksaurus) |
| 87 |
{ |
| 88 |
[m_aiksaurus dealloc]; |
| 89 |
m_aiksaurus = nil; |
| 90 |
} |
| 91 |
[super dealloc]; |
| 92 |
} |
| 93 |
|
| 94 |
- (void)lookupWord:(NSString *)word |
| 95 |
{ |
| 96 |
[oSearchField setStringValue:word]; |
| 97 |
|
| 98 |
[self aSearchField:self]; |
| 99 |
} |
| 100 |
|
| 101 |
- (void)sync |
| 102 |
{ |
| 103 |
if (m_aiksaurus == nil) |
| 104 |
return; |
| 105 |
|
| 106 |
if ([m_aiksaurus historyCanGoBack]) |
| 107 |
[oBack setEnabled:YES]; |
| 108 |
else |
| 109 |
[oBack setEnabled:NO]; |
| 110 |
|
| 111 |
if ([m_aiksaurus historyCanGoForward]) |
| 112 |
[oForward setEnabled:YES]; |
| 113 |
else |
| 114 |
[oForward setEnabled:NO]; |
| 115 |
|
| 116 |
if ([m_aiksaurus word]) |
| 117 |
[oHistory setEnabled:YES]; |
| 118 |
else |
| 119 |
[oHistory setEnabled:NO]; |
| 120 |
|
| 121 |
[oResultsTable reloadData]; |
| 122 |
} |
| 123 |
|
| 124 |
- (IBAction)aBack:(id)sender |
| 125 |
{ |
| 126 |
if ([m_aiksaurus historyBack]) |
| 127 |
{ |
| 128 |
[oHistory selectItemWithTitle:[m_aiksaurus word]]; |
| 129 |
[self sync]; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
- (IBAction)aForward:(id)sender |
| 134 |
{ |
| 135 |
if ([m_aiksaurus historyForward]) |
| 136 |
{ |
| 137 |
[oHistory selectItemWithTitle:[m_aiksaurus word]]; |
| 138 |
[self sync]; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
- (IBAction)aHistory:(id)sender |
| 143 |
{ |
| 144 |
NSString * old_word = [m_aiksaurus word]; |
| 145 |
NSString * new_word = [oHistory titleOfSelectedItem]; |
| 146 |
|
| 147 |
if (old_word == nil) |
| 148 |
{ |
| 149 |
if ([new_word length]) |
| 150 |
{ |
| 151 |
[m_aiksaurus lookupWord:new_word reorderHistory:NO]; |
| 152 |
[self sync]; |
| 153 |
} |
| 154 |
} |
| 155 |
else if ([new_word length]) |
| 156 |
{ |
| 157 |
if ([new_word isEqualToString:old_word] == NO) |
| 158 |
{ |
| 159 |
[m_aiksaurus lookupWord:new_word reorderHistory:NO]; |
| 160 |
[self sync]; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
- (IBAction)aDoubleClick:(id)sender |
| 166 |
{ |
| 167 |
NSString * value = [m_aiksaurus clickColumn:[oResultsTable clickedColumn] atRow:[oResultsTable clickedRow]]; |
| 168 |
|
| 169 |
if (value) |
| 170 |
{ |
| 171 |
[oSearchField setStringValue:value]; |
| 172 |
[self aSearchField:sender]; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
- (IBAction)aResultsTable:(id)sender |
| 177 |
{ |
| 178 |
NSString * value = [m_aiksaurus clickColumn:[oResultsTable clickedColumn] atRow:[oResultsTable clickedRow]]; |
| 179 |
|
| 180 |
if (value) |
| 181 |
{ |
| 182 |
[oSearchField setStringValue:value]; |
| 183 |
[oResultsTable reloadData]; |
| 184 |
} |
| 185 |
[oStatus setStringValue:@""]; |
| 186 |
} |
| 187 |
|
| 188 |
- (IBAction)aSearch:(id)sender |
| 189 |
{ |
| 190 |
[self aSearchField:sender]; |
| 191 |
} |
| 192 |
|
| 193 |
- (IBAction)aSearchField:(id)sender |
| 194 |
{ |
| 195 |
NSString * word = [oSearchField stringValue]; |
| 196 |
|
| 197 |
if (word) |
| 198 |
{ |
| 199 |
if ([m_aiksaurus lookupWord:word reorderHistory:YES]) |
| 200 |
{ |
| 201 |
[oHistory insertItemWithTitle:word atIndex:0]; |
| 202 |
[oHistory selectItemAtIndex:0]; |
| 203 |
[self sync]; |
| 204 |
[oStatus setTextColor:[NSColor blackColor]]; |
| 205 |
[oStatus setStringValue:@"Next please..."]; |
| 206 |
} |
| 207 |
else if ([m_aiksaurus okay]) |
| 208 |
{ |
| 209 |
[self sync]; |
| 210 |
[oStatus setTextColor:[NSColor blueColor]]; |
| 211 |
[oStatus setStringValue:@"No synonyms known. Nearby words:"]; |
| 212 |
} |
| 213 |
else |
| 214 |
{ |
| 215 |
[oStatus setTextColor:[NSColor redColor]]; |
| 216 |
[oStatus setStringValue:[m_aiksaurus lastError]]; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
- (IBAction)aInsert:(id)sender |
| 222 |
{ |
| 223 |
if (m_target) |
| 224 |
objc_msgSend (m_target, m_action, [oSearchField stringValue]); |
| 225 |
} |
| 226 |
|
| 227 |
/* and, where target's action method has the form @selector(insertWord:), i.e. |
| 228 |
- (void)insertWord:(NSString *)word; |
| 229 |
*/ |
| 230 |
- (void)setTargetForInsert:(id)target withAction:(SEL)action |
| 231 |
{ |
| 232 |
m_target = target; |
| 233 |
m_action = action; |
| 234 |
|
| 235 |
if (m_target) |
| 236 |
[oInsert setEnabled:YES]; |
| 237 |
else |
| 238 |
[oInsert setEnabled:NO]; |
| 239 |
} |
| 240 |
|
| 241 |
@end |