Ignore:
Timestamp:
03/31/03 16:47:12 (21 years ago)
Author:
Nicholas Riley
Message:

PSPreferencesController.[hm]: Manage preferences window, just like in
HostLauncher (and DockCam, I guess, though I didn't check.)

NJRHotKeyField.[hm], NJRHotKeyFieldCell.[hm]: Implements a NSTextField
subclass which intercepts every keyboard event it can, and turns it
into a human-readable representation. Don't ask me how many hours of
work this was.

English.lproj/MainMenu.nib: Hook up Preferences menu item.

English.lproj/Preferences.nib: Simple Preferences panel. One
NJRHotKeyField, one button, a couple of static text fields.

NSString-NJRExtensions.[hm]: Added method from HostLauncher (modified
to output attributed string, as it's needed in order to get the right
mix of fonts), -keyEquivalentAttributedStringWithModifierMask:.
Greatly broadened the number of keys which this method can process to
pretty much the entire extended keyboard.

NSFont-NJRExtensions.[hm]: Provide a class method for obtaining a
theme font as a NSFont.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Cocoa/Pester/Source/NSString-NJRExtensions.m

    r102 r129  
    88
    99#import "NSString-NJRExtensions.h"
     10#import "NSFont-NJRExtensions.h"
     11#include <Carbon/Carbon.h>
    1012
    1113@implementation NSString (NJRExtensions)
     14
     15+ (NSString *)stringWithCharacter:(unichar)character;
     16{
     17    return [self stringWithCharacters: &character length: 1];
     18}
    1219
    1320- (NSAttributedString *)attributedStringWithFont:(NSFont *)font;
     
    2936{
    3037    return [self attributedStringWithFont: [NSFont boldSystemFontOfSize: [NSFont smallSystemFontSize]]];
     38}
     39
     40static unichar combiningHelpChar[] = {0x003F, 0x20DD};
     41
     42static NSFont *menuItemCmdKeyFont = nil;
     43static NSFont *menuItemFont = nil;
     44
     45static void initialize() {
     46    if (menuItemCmdKeyFont != nil) return;
     47
     48    menuItemCmdKeyFont = [[NSFont themeFont: kThemeMenuItemCmdKeyFont] retain];
     49    menuItemFont = [[NSFont themeFont: kThemeMenuItemFont] retain];
     50}
     51
     52- (NSString *)keyEquivalentString;
     53{
     54    if ([self length] != 0) {
     55        const char *str = [self UTF8String];
     56        unichar keyChar;
     57        if (str[1] != '\0') {
     58            keyChar = [self characterAtIndex: 0];
     59            switch (keyChar) {
     60                case NSUpArrowFunctionKey: keyChar = 0x21E1; break;
     61                case NSDownArrowFunctionKey: keyChar = 0x21E3; break;
     62                case NSLeftArrowFunctionKey: keyChar = 0x21E0; break;
     63                case NSRightArrowFunctionKey: keyChar = 0x21E2; break;
     64                case NSInsertFunctionKey:
     65                    return [NSString stringWithCharacters: combiningHelpChar length: 2];
     66                case NSDeleteFunctionKey: keyChar = 0x2326; break;
     67                case NSHomeFunctionKey: keyChar = 0x2196; break;
     68                case NSEndFunctionKey: keyChar = 0x2198; break;
     69                case NSPageUpFunctionKey: keyChar = 0x21DE; break;
     70                case NSPageDownFunctionKey: keyChar = 0x21DF; break;
     71                case NSClearLineFunctionKey: keyChar = 0x2327; break;
     72                default:
     73                if (keyChar >= NSF1FunctionKey && keyChar <= NSF35FunctionKey) {
     74                    return [NSString stringWithFormat: @"F%u", keyChar - NSF1FunctionKey + 1];
     75                }
     76                return [NSString stringWithFormat: @"[unknown %lX]", keyChar];
     77            }
     78        } else if (str[0] >= 'A' && str[0] <= 'Z') {
     79            return self;
     80        } else if (str[0] >= 'a' && str[0] <= 'z') return [self uppercaseString];
     81        else switch (str[0]) {
     82            case '\t': keyChar = 0x21e5; break;
     83            case '\r': keyChar = 0x21a9; break;
     84            case '\e': keyChar = 0x238b; break;
     85            case ' ': keyChar = 0x2423; break;
     86            case 0x7f: keyChar = 0x232b; break; // delete
     87            case 0x03: keyChar = 0x2324; break; // enter
     88            case 0x19: keyChar = 0x21e4; break; // backtab
     89            case 0: return @"";
     90            // case '': keyChar = 0x; break;
     91            default: return self; // return [NSString stringWithFormat: @"[huh? %x]", (int)str[0]]; //
     92        }
     93        return [NSString stringWithCharacter: keyChar];
     94    }
     95    return self;
     96}
     97
     98- (NSAttributedString *)keyEquivalentAttributedStringWithModifierMask:(unsigned int)modifierMask;
     99{
     100    initialize();
     101    NSString *keyEquivalentStringNoMask = [self keyEquivalentString];
     102    NSAttributedString *keyEquivalentAttributedString =
     103        [[NSString stringWithFormat: @"%@%@%@%@%@",
     104            (modifierMask & NSControlKeyMask) ? [NSString stringWithCharacter: kControlUnicode] : @"",
     105            (modifierMask & NSAlternateKeyMask) ? [NSString stringWithCharacter: kOptionUnicode] : @"",
     106            (modifierMask & NSShiftKeyMask) ? [NSString stringWithCharacter: kShiftUnicode] : @"",
     107            (modifierMask & NSCommandKeyMask) ? [NSString stringWithCharacter: kCommandUnicode] : @"",
     108                keyEquivalentStringNoMask]
     109        attributedStringWithFont: menuItemCmdKeyFont];
     110    unsigned noMaskLength = [keyEquivalentStringNoMask length];
     111    if (noMaskLength > 3 || // Fxx
     112        (noMaskLength == 1 && [keyEquivalentStringNoMask characterAtIndex: 0] <= 0x7F)) {
     113        NSMutableAttributedString *astr = [keyEquivalentAttributedString mutableCopy];
     114        [astr setAttributes: [NSDictionary dictionaryWithObject: menuItemFont forKey: NSFontAttributeName] range: NSMakeRange([astr length] - noMaskLength, noMaskLength)];
     115        keyEquivalentAttributedString = [[astr copy] autorelease];
     116        [astr release];
     117    }
     118    return keyEquivalentAttributedString;
    31119}
    32120
Note: See TracChangeset for help on using the changeset viewer.