Ignore:
Timestamp:
05/31/03 05:04:00 (19 years ago)
Author:
Nicholas Riley
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ICeCoffEE/ICeCoffEE/ICeCoffEE.m

    r106 r139  
    5656}
    5757
     58CFStringRef ICCF_CopyAppName() {
     59    ProcessSerialNumber psn = {0, kCurrentProcess};
     60    CFStringRef appName = NULL;
     61    CopyProcessName(&psn, &appName);
     62    if (appName == NULL) return CFSTR("(unknown)");
     63    return appName;
     64}
     65
    5866BOOL ICCF_EventIsCommandMouseDown(NSEvent *e) {
    5967    return ([e type] == NSLeftMouseDown && ([e modifierFlags] & NSCommandKeyMask) != 0 && [e clickCount] == 1);
     
    8391
    8492        [set autorelease];
     93        [set formUnionWithCharacterSet: [[NSCharacterSet characterSetWithRange: NSMakeRange(0, 128)] invertedSet]]; // remove non-ASCII characters
    8594        [set formUnionWithCharacterSet: [NSCharacterSet punctuationCharacterSet]];
    8695        [set removeCharactersInString: @";/?:@&=+$,-_.!~*'()%#"]; // RFC 2396 ¤2.2, 2.3, 2.4, plus #
     
    207216        ICLog(@"Parsing URL |%@|", string);
    208217
     218        NSCAssert([string canBeConvertedToEncoding: NSASCIIStringEncoding], @"No URL is selected");
     219
    209220        urlData = (char *)malloc( (range->length + 1) * sizeof(char));
    210221        NSCAssert(urlData != NULL, @"Internal error: canÕt allocate memory for URL string");
     
    288299    int result = NSRunAlertPanel(ICCF_LocalizedString(@"AlertTitle"), ICCF_LocalizedString(@"AlertMessage%@"), nil, nil, ICCF_LocalizedString(@"AlertDisableButton"), e);
    289300    if (result != NSAlertDefaultReturn) {
    290         result = NSRunAlertPanel(ICCF_LocalizedString(@"DisableAlertTitle"), ICCF_LocalizedString(@"DisableAlertMessage"), ICCF_LocalizedString(@"DisableAlertDisableButton"), ICCF_LocalizedString(@"DisableAlertDontDisableButton"), nil);
     301        result = NSRunAlertPanel(ICCF_LocalizedString(@"DisableAlertTitle"), ICCF_LocalizedString(@"DisableAlertMessage%@"), ICCF_LocalizedString(@"DisableAlertDisableButton"), ICCF_LocalizedString(@"DisableAlertDontDisableButton"), nil,
     302           [(NSString *)ICCF_CopyAppName() autorelease]);
    291303        if (result == NSAlertDefaultReturn)
    292304            ICCF_enabled = NO;
     
    391403    NSMenuItem *servicesItem;
    392404    NSMenu *servicesMenu;
    393     // XXX better to just use [[NSApp servicesMenu] title]?  That grabs the title from the existing Services submenu.
     405    // don't want to use [[NSApp servicesMenu] title] because the Services menu may not have been created yet
    394406    NSString *servicesTitle = [[NSBundle bundleWithIdentifier: @"com.apple.AppKit"] localizedStringForKey: @"Services" value: nil table: @"ServicesMenu"];
    395407    if (servicesTitle == nil) {
    396         ICLog(@"CanÕt get localized text for ÒServicesÓ in AppKit.framework");
     408        ICLog(@"CanÕt get localized text for 'Services' in AppKit.framework");
    397409        servicesTitle = @"Services";
    398410    }
     
    403415    [servicesItem setRepresentedObject: ICCF_SERVICES_ITEM];
    404416    [servicesMenu release];
    405     return servicesItem;
     417    return [servicesItem autorelease];
     418}
     419
     420static const unichar UNICHAR_BLACK_RIGHT_POINTING_SMALL_TRIANGLE = 0x25b8;
     421
     422// returns YES if menu contains useful items, NO otherwise
     423BOOL ICCF_ConsolidateServicesMenu(NSMenu *menu) {
     424    [menu update]; // doesn't propagate to submenus, so we need to do this first
     425    NSEnumerator *enumerator = [[menu itemArray] objectEnumerator];
     426    NSMenuItem *menuItem;
     427    NSMenu *submenu;
     428    BOOL shouldKeepItem = NO, shouldKeepMenu = NO;
     429
     430    while ( (menuItem = [enumerator nextObject]) != nil) {
     431        if ( (submenu = [menuItem submenu]) != nil) {
     432            shouldKeepItem = ICCF_ConsolidateServicesMenu(submenu);
     433            if (shouldKeepItem && [submenu numberOfItems] == 1) { // consolidate
     434                NSMenuItem *serviceItem = [[submenu itemAtIndex: 0] retain];
     435                [serviceItem setTitle:
     436                    [NSString stringWithFormat: @"%@ %@ %@", [menuItem title], [NSString stringWithCharacters: &UNICHAR_BLACK_RIGHT_POINTING_SMALL_TRIANGLE length: 1], [serviceItem title]]];
     437
     438                int serviceIndex = [menu indexOfItem: menuItem];
     439                [submenu removeItemAtIndex: 0]; // can't have item in two menus
     440                [menu removeItemAtIndex: serviceIndex];
     441                [menu insertItem: serviceItem atIndex: serviceIndex];
     442                [serviceItem release];
     443            }
     444        } else {
     445            shouldKeepItem = [menuItem isEnabled];
     446        }
     447        if (shouldKeepItem) {
     448            shouldKeepMenu = YES;
     449        } else {
     450            [menu removeItem: menuItem];
     451        }
     452    }
     453
     454    return shouldKeepMenu;
     455}
     456
     457NSMenuItem *ICCF_ContextualServicesMenuItem() {
     458    NSMenuItem *servicesItem = ICCF_ServicesMenuItem();
     459    if (ICCF_ConsolidateServicesMenu([servicesItem submenu]))
     460        return servicesItem;
     461    else
     462        return nil;
    406463}
    407464
     
    413470}
    414471
    415 NSMenu *ICCF_MenuForEvent(NSTextView *self, NSMenu *contextMenu, NSEvent *e) {   
     472NSMenu *ICCF_MenuForEvent(NSTextView *self, NSMenu *contextMenu, NSEvent *e) {
    416473    if (contextMenu != nil && [e type] == NSRightMouseDown || ([e type] == NSLeftMouseDown && [e modifierFlags] & NSControlKeyMask)) {
    417474        int servicesItemIndex = [contextMenu indexOfItemWithRepresentedObject: ICCF_SERVICES_ITEM];
    418         if (servicesItemIndex == -1 && ICCF_prefs.servicesInContextualMenu) {
    419             [contextMenu addItem: [NSMenuItem separatorItem]];
    420             [contextMenu addItem: ICCF_ServicesMenuItem()];
    421         } else if (servicesItemIndex != -1 && !ICCF_prefs.servicesInContextualMenu) {
     475        // always regenerate: make sure menu reflects context
     476        if (servicesItemIndex != -1) {
    422477            [contextMenu removeItemAtIndex: servicesItemIndex];
    423478            [contextMenu removeItemAtIndex: servicesItemIndex - 1];
     479        }
     480        if (ICCF_prefs.servicesInContextualMenu) {
     481            NSMenuItem *contextualServicesItem = ICCF_ContextualServicesMenuItem();
     482            if (contextualServicesItem != nil) {
     483                [contextMenu addItem: [NSMenuItem separatorItem]];
     484                [contextMenu addItem: contextualServicesItem];
     485            }
    424486        }
    425487    }
     
    447509        servicesItem = nil;
    448510    }
     511    [[NSApp servicesMenu] update]; // enable keyboard equivalents
    449512}
    450513
Note: See TracChangeset for help on using the changeset viewer.