Ignore:
Timestamp:
04/01/03 21:25:02 (21 years ago)
Author:
Nicholas Riley
Message:

PSPreferencesController.[hm]: Added support for registering hot keys;
not the most elegant thing in the world, but much better than it was
in the prototype. Triggered by +readPreferences.

NJRHotKeyField.[hm]: Replaced model components (wow, was that ever
dumb) by NJRHotKey reference, eliminating cumbersome archiving model.
Added accessors for hot key.

NJRHotKeyManager.[hm]: Ported Quentin Carnicelli's HotKeyCenter code
to use NJRHotKey, cleaned up, and removed reverse-engineered pre-10.2
support.

NJRHotKey.[hm]: New. Provides Cocoa-centric storage for
three-component hot keys, mapping from Cocoa to Carbon modifiers.

PSApplication.m: Reorganized. Added invocation of
+[PSPreferencesController readPreferences].

Fixes bug 29.

File:
1 edited

Legend:

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

    r130 r131  
    99#import "NJRHotKeyField.h"
    1010#import "NJRHotKeyFieldCell.h"
     11#import "NJRHotKey.h"
    1112#import "NSString-NJRExtensions.h"
    12 
    13 // property list keys
    14 static NSString * const PLCharacters = @"characters"; // NSString
    15 static NSString * const PLModifierFlags = @"modifierFlags"; // NSNumber
    16 static NSString * const PLKeyCode = @"keyCode"; // NSNumber
    1713
    1814static const NSRange zeroRange = {0, 0};
     
    4844}
    4945
     46#pragma mark initialize-release
     47
    5048- (id)initWithCoder:(NSCoder *)coder;
    5149{
     
    6361    return self;
    6462}
     63
     64- (void)dealloc;
     65{
     66    [hotKey release];
     67}
     68
     69#pragma mark interface updating
    6570
    6671// XXX still problems with command-A the first time
     
    8590- (void)showKeyEquivalentAttributedStringFinalized:(BOOL)finalized;
    8691{
    87     if (hotKeyCharacters == nil) {
     92    if ([hotKey characters] == nil) {
    8893        [self showStatus: @"none assigned"];
    8994        return;
    9095    }
    91     NSMutableAttributedString *equivString = [[hotKeyCharacters keyEquivalentAttributedStringWithModifierFlags: hotKeyModifierFlags] mutableCopy];
     96    NSMutableAttributedString *equivString = [[[hotKey characters] keyEquivalentAttributedStringWithModifierFlags: [hotKey modifierFlags]] mutableCopy];
    9297    [equivString addAttribute: NSParagraphStyleAttributeName
    9398                        value: (finalized ? centerAlignStyle : leftAlignStyle)
     
    98103}
    99104
    100 - (void)clearHotKey;
    101 {
    102     [hotKeyCharacters release];
    103     hotKeyCharacters = nil;
    104     hotKeyModifierFlags = 0;
    105     hotKeyCode = 0;
    106     [NSApp sendAction: [self action] to: [self target] from: self];
    107 }
     105#pragma mark event handling
    108106
    109107- (void)keyUp:(NSEvent *)theEvent;
     
    122120            [self showStatus: message != nil ? message : @"key is unavailable for use"];
    123121        } else {
    124             [hotKeyCharacters release];
    125             hotKeyCharacters = [characters retain];
    126             hotKeyModifierFlags = modifierFlags;
    127             hotKeyCode = [theEvent keyCode];
     122            [self setHotKey: [NJRHotKey hotKeyWithCharacters: characters modifierFlags: modifierFlags keyCode: [theEvent keyCode]]];
    128123            [NSApp sendAction: [self action] to: [self target] from: self];
    129124            [self showKeyEquivalentAttributedStringFinalized: ([theEvent modifierFlags] & capturedModifierMask) == 0];
     
    150145}
    151146
     147#pragma mark acccessing
     148
     149- (NJRHotKey *)hotKey;
     150{
     151    return hotKey;
     152}
     153
     154- (void)setHotKey:(NJRHotKey *)aKey;
     155{
     156    if (aKey != hotKey) {
     157        [hotKey release];
     158        hotKey = [aKey retain];
     159        [self showKeyEquivalentAttributedStringFinalized: ([[NSApp currentEvent] modifierFlags] & capturedModifierMask) == 0];
     160    }
     161}
     162
     163#pragma mark actions
     164
    152165- (IBAction)clear:(id)sender;
    153166{
    154     [self clearHotKey];
     167    [self setHotKey: nil];
     168    [NSApp sendAction: [self action] to: [self target] from: self];
    155169    [self showKeyEquivalentAttributedStringFinalized: YES];
    156170}
    157171
    158 #pragma mark property list serialization (Pester 1.1)
    159 
    160 - (NSDictionary *)propertyListRepresentation;
    161 {
    162     return [NSDictionary dictionaryWithObjectsAndKeys:
    163         hotKeyCharacters, PLCharacters,
    164         [NSNumber numberWithUnsignedInt: hotKeyModifierFlags], PLModifierFlags,
    165         [NSNumber numberWithUnsignedShort: hotKeyCode], PLKeyCode,
    166         nil];
    167 }
    168 
    169 - (void)setFromPropertyList:(NSDictionary *)dict;
    170 {
    171     NS_DURING
    172         hotKeyCharacters = [[dict objectForKey: PLCharacters] retain];
    173         hotKeyModifierFlags = [[dict objectForKey: PLModifierFlags] unsignedIntValue];
    174         hotKeyCode = [[dict objectForKey: PLKeyCode] unsignedShortValue];
    175         [self showKeyEquivalentAttributedStringFinalized: ([[NSApp currentEvent] modifierFlags] & capturedModifierMask) == 0];
    176     NS_HANDLER
    177         [self clear: nil];
    178     NS_ENDHANDLER
    179 }
    180 
    181172@end
Note: See TracChangeset for help on using the changeset viewer.