Changeset 621 for trunk


Ignore:
Timestamp:
03/02/10 08:03:59 (15 years ago)
Author:
Nicholas Riley
Message:

Date autocomplete (rough).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cocoa/Pester/Source/PSAlarmSetController.m

    r613 r621  
    583583}
    584584
     585- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int *)idx;
     586{
     587    if (control != timeDate)
     588        return nil;
     589
     590    NSString *partialMatch = [textView string];
     591    unsigned partialLength = [partialMatch length];
     592    NSMutableArray *completions = [[timeDateCompletions itemTitles] mutableCopy];
     593    for (int i = [completions count] - 1 ; i >= 0 ; i--) {
     594        NSString *completion = [completions objectAtIndex: i];
     595        unsigned length = [completion length];
     596        if (partialLength == 0) {
     597            if (length > 0)
     598                continue;
     599        } else if (length >= partialLength &&
     600            [partialMatch compare:
     601             [completion substringToIndex: partialLength] options:NSCaseInsensitiveSearch] == NSOrderedSame) {
     602            continue;
     603        }
     604       
     605        [completions removeObjectAtIndex: i];
     606    }
     607    NSLog(@"%@ %d %@", partialMatch, partialLength, completions);
     608    return [completions autorelease];
     609}
     610
    585611@end
    586612
     
    603629- (void)controlTextDidEndEditing:(NSNotification *)notification;
    604630{
    605     if ([notification object] != timeOfDay)
     631    NSControl *control = [notification object];
     632
     633    if (control != timeOfDay)
    606634        return;
    607 
     635   
    608636    // if date is today and we've picked a time before now, set the date for tomorrow
    609637    NSDate *dateTime = [NSCalendarDate dateWithDate: [timeDate objectValue] atTime: [timeOfDay objectValue]];
     
    621649}
    622650
     651static BOOL completingTimeDate = NO;
     652static NSString *lastTimeDateString = nil;
     653
     654- (void)controlTextDidBeginEditing:(NSNotification *)notification;
     655{
     656    NSControl *control = [notification object];
     657   
     658    if (control != timeOfDay)
     659        return;
     660   
     661    [lastTimeDateString release];
     662    lastTimeDateString = [[[[notification userInfo] objectForKey:@"NSFieldEditor"] string] copy];
     663}
     664
    623665- (void)controlTextDidChange:(NSNotification *)notification;
    624666{
    625667    // NSLog(@"UPDATING FROM controlTextDidChange: %@", [notification object]);
     668    if ([notification object] == timeDate) {
     669        if (!completingTimeDate) {
     670            NSText *fieldEditor = [[notification userInfo] objectForKey:@"NSFieldEditor"];
     671            NSString *editingString = [fieldEditor string];
     672            if ([editingString length] > [lastTimeDateString length])
     673                completingTimeDate = YES;
     674            [lastTimeDateString release];
     675            lastTimeDateString = [editingString copy];
     676            if (completingTimeDate) {
     677                [fieldEditor complete: nil];
     678                completingTimeDate = NO;
     679            }
     680        }
     681    }
    626682    [self update: [notification object]];
     683}
     684
     685// XXX need to override NSTextView; when completing with space or tab, we don't get a selector, which causes usability problems with tab and bugs with space
     686- (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector;
     687{
     688    if (control != timeDate)
     689        return NO;
     690   
     691    if (commandSelector == @selector(moveDown:)) {
     692        completingTimeDate = YES;
     693        [textView complete: nil];
     694        completingTimeDate = NO;
     695        return YES;
     696    }
     697   
     698    return NO;
    627699}
    628700
Note: See TracChangeset for help on using the changeset viewer.