| 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 |
#include "AiksaurusCocoa.h" |
| 23 |
#include "Aiksaurus.h" |
| 24 |
|
| 25 |
@implementation AiksaurusCocoaMeaning |
| 26 |
|
| 27 |
- (id)initWithoutTitles |
| 28 |
{ |
| 29 |
[super init]; |
| 30 |
|
| 31 |
m_titles = NO; |
| 32 |
|
| 33 |
m_title_1 = nil; |
| 34 |
m_title_2 = nil; |
| 35 |
|
| 36 |
m_synonyms = [NSMutableArray array]; |
| 37 |
|
| 38 |
[m_synonyms retain]; |
| 39 |
|
| 40 |
m_rows = 0; |
| 41 |
|
| 42 |
return self; |
| 43 |
} |
| 44 |
|
| 45 |
- (id)initWithTitle:(NSString *)title withAlternativeTitle:(NSString *)alt |
| 46 |
{ |
| 47 |
[super init]; |
| 48 |
|
| 49 |
m_titles = YES; |
| 50 |
|
| 51 |
m_title_1 = title; |
| 52 |
m_title_2 = alt; |
| 53 |
|
| 54 |
[m_title_1 retain]; |
| 55 |
[m_title_2 retain]; |
| 56 |
|
| 57 |
m_synonyms = [NSMutableArray array]; |
| 58 |
|
| 59 |
[m_synonyms retain]; |
| 60 |
|
| 61 |
m_rows = 1; |
| 62 |
|
| 63 |
return self; |
| 64 |
} |
| 65 |
|
| 66 |
- (void)dealloc |
| 67 |
{ |
| 68 |
if (m_titles) |
| 69 |
{ |
| 70 |
[m_title_1 release]; |
| 71 |
[m_title_2 release]; |
| 72 |
} |
| 73 |
[m_synonyms release]; |
| 74 |
|
| 75 |
[super dealloc]; |
| 76 |
} |
| 77 |
|
| 78 |
- (void)synonymAdd:(NSString *)synonym |
| 79 |
{ |
| 80 |
[m_synonyms addObject:synonym]; |
| 81 |
|
| 82 |
if (m_titles) |
| 83 |
m_rows = 1 + ((int) [m_synonyms count] + 3) / 4; |
| 84 |
else |
| 85 |
m_rows = ((int) [m_synonyms count] + 3) / 4; |
| 86 |
} |
| 87 |
|
| 88 |
- (NSString *)synonymAtPosition:(unsigned)position |
| 89 |
{ |
| 90 |
if ((position >= 0) && (position < [m_synonyms count])) |
| 91 |
{ |
| 92 |
return (NSString *) [m_synonyms objectAtIndex:position]; |
| 93 |
} |
| 94 |
return nil; |
| 95 |
} |
| 96 |
|
| 97 |
- (unsigned)synonyms |
| 98 |
{ |
| 99 |
return [m_synonyms count]; |
| 100 |
} |
| 101 |
|
| 102 |
- (NSString *)clickColumn:(int)colIndex atRow:(int)rowIndex |
| 103 |
{ |
| 104 |
NSString * value = nil; |
| 105 |
|
| 106 |
if (rowIndex || !m_titles) |
| 107 |
{ |
| 108 |
int index = colIndex; |
| 109 |
if (m_titles) |
| 110 |
index += (rowIndex - 1) * 4; |
| 111 |
else |
| 112 |
index += (rowIndex ) * 4; |
| 113 |
if (index < (int) [m_synonyms count]) |
| 114 |
value = (NSString *) [m_synonyms objectAtIndex:index]; |
| 115 |
} |
| 116 |
return value; |
| 117 |
} |
| 118 |
|
| 119 |
- (int)numberOfRowsInTableView |
| 120 |
{ |
| 121 |
return m_rows; |
| 122 |
} |
| 123 |
|
| 124 |
- (NSString *)objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex |
| 125 |
{ |
| 126 |
NSString * value = @""; |
| 127 |
NSString * col = (NSString *) [aTableColumn identifier]; |
| 128 |
|
| 129 |
int colIndex = 3; |
| 130 |
|
| 131 |
if ([col isEqualToString:@"1"]) |
| 132 |
colIndex = 0; |
| 133 |
else if ([col isEqualToString:@"2"]) |
| 134 |
colIndex = 1; |
| 135 |
else if ([col isEqualToString:@"3"]) |
| 136 |
colIndex = 2; |
| 137 |
|
| 138 |
NSTextFieldCell * cell = (NSTextFieldCell *) [aTableColumn dataCell]; |
| 139 |
|
| 140 |
if (rowIndex || !m_titles) |
| 141 |
{ |
| 142 |
[cell setDrawsBackground:NO]; |
| 143 |
|
| 144 |
int index = colIndex; |
| 145 |
if (m_titles) |
| 146 |
index += (rowIndex - 1) * 4; |
| 147 |
else |
| 148 |
index += (rowIndex ) * 4; |
| 149 |
if (index < (int) [m_synonyms count]) |
| 150 |
value = (NSString *) [m_synonyms objectAtIndex:index]; |
| 151 |
} |
| 152 |
else |
| 153 |
{ |
| 154 |
[cell setDrawsBackground:YES]; |
| 155 |
[cell setBackgroundColor:[NSColor controlHighlightColor]]; |
| 156 |
|
| 157 |
if (colIndex == 0) |
| 158 |
value = m_title_1; |
| 159 |
else if (colIndex == 2) |
| 160 |
value = m_title_2; |
| 161 |
} |
| 162 |
return value; |
| 163 |
} |
| 164 |
|
| 165 |
@end |
| 166 |
|
| 167 |
@implementation AiksaurusCocoa |
| 168 |
|
| 169 |
- (id)init |
| 170 |
{ |
| 171 |
[super init]; |
| 172 |
|
| 173 |
m_aiksaurus = 0; |
| 174 |
|
| 175 |
m_length = 0; |
| 176 |
m_position = 0; |
| 177 |
|
| 178 |
m_history = [NSMutableArray array]; |
| 179 |
m_meanings = [NSMutableArray array]; |
| 180 |
|
| 181 |
[m_history retain]; |
| 182 |
[m_meanings retain]; |
| 183 |
|
| 184 |
m_rows = 0; |
| 185 |
|
| 186 |
m_highlight_row = -1; |
| 187 |
m_highlight_col = -1; |
| 188 |
|
| 189 |
NSBundle * bundle = [NSBundle bundleForClass:[self class]]; |
| 190 |
|
| 191 |
NSString * path_meanings = [bundle pathForResource:@"meanings" ofType:@"dat"]; |
| 192 |
NSString * path_words = [bundle pathForResource:@"words" ofType:@"dat"]; |
| 193 |
|
| 194 |
if (path_meanings && path_words) |
| 195 |
{ |
| 196 |
try |
| 197 |
{ |
| 198 |
Aiksaurus * aik = new Aiksaurus([path_meanings UTF8String], |
| 199 |
[path_words UTF8String]); |
| 200 |
m_aiksaurus = reinterpret_cast<void *>(aik); |
| 201 |
} |
| 202 |
catch (...) |
| 203 |
{ |
| 204 |
m_aiksaurus = 0; |
| 205 |
} |
| 206 |
} |
| 207 |
return self; |
| 208 |
} |
| 209 |
|
| 210 |
- (void)dealloc |
| 211 |
{ |
| 212 |
if (m_aiksaurus) |
| 213 |
{ |
| 214 |
Aiksaurus * aik = reinterpret_cast<Aiksaurus *>(m_aiksaurus); |
| 215 |
delete aik; |
| 216 |
m_aiksaurus = 0; |
| 217 |
} |
| 218 |
|
| 219 |
[m_history release]; |
| 220 |
[m_meanings release]; |
| 221 |
|
| 222 |
[super dealloc]; |
| 223 |
} |
| 224 |
|
| 225 |
- (BOOL)okay |
| 226 |
{ |
| 227 |
if (!m_aiksaurus) return NO; |
| 228 |
|
| 229 |
Aiksaurus * aik = reinterpret_cast<Aiksaurus *>(m_aiksaurus); |
| 230 |
|
| 231 |
const char * aik_error = aik->error (); |
| 232 |
|
| 233 |
return (*aik_error ? NO : YES); |
| 234 |
} |
| 235 |
|
| 236 |
- (NSString *)lastError |
| 237 |
{ |
| 238 |
if (!m_aiksaurus) return @"Error: Couldn't create Aiksaurus library!"; |
| 239 |
|
| 240 |
Aiksaurus * aik = reinterpret_cast<Aiksaurus *>(m_aiksaurus); |
| 241 |
|
| 242 |
const char * aik_error = aik->error (); |
| 243 |
|
| 244 |
return (*aik_error ? [NSString stringWithCString:aik_error] : nil); |
| 245 |
} |
| 246 |
|
| 247 |
- (BOOL)historyBack |
| 248 |
{ |
| 249 |
BOOL change = [self historyCanGoBack]; |
| 250 |
|
| 251 |
if (change) |
| 252 |
[self lookupWord:(NSString *)[m_history objectAtIndex:(m_position-1)] reorderHistory:NO]; |
| 253 |
|
| 254 |
return change; |
| 255 |
} |
| 256 |
|
| 257 |
- (BOOL)historyForward |
| 258 |
{ |
| 259 |
BOOL change = [self historyCanGoForward]; |
| 260 |
|
| 261 |
if (change) |
| 262 |
[self lookupWord:(NSString *)[m_history objectAtIndex:(m_position+1)] reorderHistory:NO]; |
| 263 |
|
| 264 |
return change; |
| 265 |
} |
| 266 |
|
| 267 |
- (BOOL)historyCanGoBack |
| 268 |
{ |
| 269 |
return (m_position ? YES : NO); |
| 270 |
} |
| 271 |
|
| 272 |
- (BOOL)historyCanGoForward |
| 273 |
{ |
| 274 |
return ((m_position + 1 < (int) m_length) ? YES : NO); |
| 275 |
} |
| 276 |
|
| 277 |
- (void)setWord:(NSString *)newword reorderHistory:(BOOL)reorder |
| 278 |
{ |
| 279 |
if (newword == nil) return; |
| 280 |
if ([newword length] == 0) return; |
| 281 |
|
| 282 |
NSString * current = [self word]; |
| 283 |
|
| 284 |
if (current == nil) |
| 285 |
{ |
| 286 |
[m_history addObject:newword]; |
| 287 |
|
| 288 |
m_length = [m_history count]; |
| 289 |
m_position = 0; |
| 290 |
} |
| 291 |
else if ([current isEqualToString:newword] == NO) |
| 292 |
{ |
| 293 |
unsigned position = m_length; |
| 294 |
|
| 295 |
for (unsigned i = 0; i < m_length; i++) |
| 296 |
{ |
| 297 |
NSString * str = (NSString *) [m_history objectAtIndex:i]; |
| 298 |
if ([newword isEqualToString:str]) |
| 299 |
{ |
| 300 |
position = i; |
| 301 |
break; |
| 302 |
} |
| 303 |
} |
| 304 |
if (position == m_length) |
| 305 |
{ |
| 306 |
[m_history addObject:newword]; |
| 307 |
m_length = [m_history count]; |
| 308 |
m_position = m_length - 1; |
| 309 |
} |
| 310 |
else if (reorder && (position < m_length - 1)) |
| 311 |
{ |
| 312 |
[m_history removeObjectAtIndex:position]; |
| 313 |
[m_history addObject:newword]; |
| 314 |
m_length = [m_history count]; |
| 315 |
m_position = m_length - 1; |
| 316 |
} |
| 317 |
else |
| 318 |
{ |
| 319 |
m_position = position; |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
- (NSString *)word |
| 325 |
{ |
| 326 |
NSString * current = nil; |
| 327 |
|
| 328 |
if (m_length) current = (NSString *) [m_history objectAtIndex:m_position]; |
| 329 |
|
| 330 |
return current; |
| 331 |
} |
| 332 |
|
| 333 |
- (BOOL)lookupWord:(NSString *)newword reorderHistory:(BOOL)reorder |
| 334 |
{ |
| 335 |
m_highlight_row = -1; |
| 336 |
m_highlight_col = -1; |
| 337 |
|
| 338 |
if (!m_aiksaurus || !newword) |
| 339 |
return NO; |
| 340 |
if ([newword length] == 0) |
| 341 |
return NO; |
| 342 |
|
| 343 |
NSString * current = [self word]; |
| 344 |
if (current) |
| 345 |
if ([current isEqualToString:newword]) |
| 346 |
return YES; |
| 347 |
|
| 348 |
[m_meanings removeAllObjects]; |
| 349 |
|
| 350 |
Aiksaurus * aik = reinterpret_cast<Aiksaurus *>(m_aiksaurus); |
| 351 |
|
| 352 |
if (!aik->find ([newword UTF8String])) |
| 353 |
{ |
| 354 |
AiksaurusCocoaMeaning * acm = nil; |
| 355 |
acm = [[AiksaurusCocoaMeaning alloc] initWithoutTitles]; |
| 356 |
|
| 357 |
[m_meanings addObject:acm]; |
| 358 |
[acm release]; // ?? |
| 359 |
|
| 360 |
for (const char * s = aik->similar (); *s; s = aik->similar ()) |
| 361 |
{ |
| 362 |
[acm synonymAdd:[NSString stringWithUTF8String:s]]; |
| 363 |
} |
| 364 |
m_rows = [acm numberOfRowsInTableView]; |
| 365 |
|
| 366 |
return NO; |
| 367 |
} |
| 368 |
|
| 369 |
[self setWord:newword reorderHistory:reorder]; |
| 370 |
|
| 371 |
int prev_meaning = -1; |
| 372 |
int this_meaning = -1; |
| 373 |
|
| 374 |
for (const char * r = aik->next (this_meaning); *r; r = aik->next (this_meaning)) |
| 375 |
{ |
| 376 |
if (prev_meaning != this_meaning) |
| 377 |
{ |
| 378 |
NSString * title_1 = [NSString stringWithUTF8String:r]; |
| 379 |
r = aik->next (this_meaning); |
| 380 |
NSString * title_2 = [NSString stringWithUTF8String:r]; |
| 381 |
|
| 382 |
AiksaurusCocoaMeaning * acm = nil; |
| 383 |
acm = [[AiksaurusCocoaMeaning alloc] initWithTitle:title_1 withAlternativeTitle:title_2]; |
| 384 |
|
| 385 |
[m_meanings addObject:acm]; |
| 386 |
[acm release]; // ?? |
| 387 |
|
| 388 |
prev_meaning = this_meaning; |
| 389 |
} |
| 390 |
else |
| 391 |
{ |
| 392 |
AiksaurusCocoaMeaning * acm = (AiksaurusCocoaMeaning *) [m_meanings lastObject]; |
| 393 |
if (acm) [acm synonymAdd:[NSString stringWithUTF8String:r]]; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
/* calculate number of rows in standard table layout; cache for efficient use later |
| 398 |
*/ |
| 399 |
m_rows = 0; |
| 400 |
|
| 401 |
int count = (int) [m_meanings count]; |
| 402 |
|
| 403 |
for (int i = 0; i < count; i++) |
| 404 |
{ |
| 405 |
AiksaurusCocoaMeaning * acm = (AiksaurusCocoaMeaning *) [m_meanings objectAtIndex:i]; |
| 406 |
m_rows += [acm numberOfRowsInTableView]; |
| 407 |
} |
| 408 |
return YES; |
| 409 |
} |
| 410 |
|
| 411 |
- (AiksaurusCocoaMeaning *)meaningAtPosition:(unsigned)position |
| 412 |
{ |
| 413 |
if ((position >= 0) && (position < [m_meanings count])) |
| 414 |
{ |
| 415 |
return (AiksaurusCocoaMeaning *) [m_history objectAtIndex:position]; |
| 416 |
} |
| 417 |
return nil; |
| 418 |
} |
| 419 |
|
| 420 |
- (unsigned)meanings |
| 421 |
{ |
| 422 |
return [m_meanings count]; |
| 423 |
} |
| 424 |
|
| 425 |
- (NSString *)clickColumn:(int)colIndex atRow:(int)rowIndex |
| 426 |
{ |
| 427 |
NSString * value = nil; |
| 428 |
|
| 429 |
int rows = 0; |
| 430 |
|
| 431 |
unsigned count = [m_meanings count]; |
| 432 |
|
| 433 |
for (unsigned i = 0; i < count; i++) |
| 434 |
{ |
| 435 |
AiksaurusCocoaMeaning * acm = (AiksaurusCocoaMeaning *) [m_meanings objectAtIndex:i]; |
| 436 |
int sub_rows = [acm numberOfRowsInTableView]; |
| 437 |
if ((rowIndex >= rows) && (rowIndex < rows + sub_rows)) |
| 438 |
{ |
| 439 |
value = [acm clickColumn:colIndex atRow:(rowIndex - rows)]; |
| 440 |
break; |
| 441 |
} |
| 442 |
rows += sub_rows; |
| 443 |
} |
| 444 |
if (value) |
| 445 |
{ |
| 446 |
m_highlight_row = rowIndex; |
| 447 |
m_highlight_col = colIndex; |
| 448 |
} |
| 449 |
return value; |
| 450 |
} |
| 451 |
|
| 452 |
- (int)numberOfRowsInTableView:(NSTableView *)aTableView |
| 453 |
{ |
| 454 |
return m_rows; |
| 455 |
} |
| 456 |
|
| 457 |
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex |
| 458 |
{ |
| 459 |
NSString * value = @""; |
| 460 |
|
| 461 |
int rows = 0; |
| 462 |
|
| 463 |
unsigned count = [m_meanings count]; |
| 464 |
|
| 465 |
for (unsigned i = 0; i < count; i++) |
| 466 |
{ |
| 467 |
AiksaurusCocoaMeaning * acm = (AiksaurusCocoaMeaning *) [m_meanings objectAtIndex:i]; |
| 468 |
int sub_rows = [acm numberOfRowsInTableView]; |
| 469 |
if ((rowIndex >= rows) && (rowIndex < rows + sub_rows)) |
| 470 |
{ |
| 471 |
value = [acm objectValueForTableColumn:aTableColumn row:(rowIndex - rows)]; |
| 472 |
break; |
| 473 |
} |
| 474 |
rows += sub_rows; |
| 475 |
} |
| 476 |
return value; |
| 477 |
} |
| 478 |
|
| 479 |
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex |
| 480 |
{ |
| 481 |
if (rowIndex == m_highlight_row) |
| 482 |
{ |
| 483 |
NSString * col = (NSString *) [aTableColumn identifier]; |
| 484 |
|
| 485 |
int colIndex = 3; |
| 486 |
|
| 487 |
if ([col isEqualToString:@"1"]) |
| 488 |
colIndex = 0; |
| 489 |
else if ([col isEqualToString:@"2"]) |
| 490 |
colIndex = 1; |
| 491 |
else if ([col isEqualToString:@"3"]) |
| 492 |
colIndex = 2; |
| 493 |
|
| 494 |
if (colIndex == m_highlight_col) |
| 495 |
{ |
| 496 |
[[aTableColumn dataCell] setDrawsBackground:YES]; |
| 497 |
[[aTableColumn dataCell] setBackgroundColor:[NSColor selectedControlColor]]; |
| 498 |
} |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(int)rowIndex |
| 503 |
{ |
| 504 |
return NO; |
| 505 |
} |
| 506 |
|
| 507 |
@end |