source: trunk/Cocoa/Pester/Source/PSPreferencesController.m @ 130

Last change on this file since 130 was 130, checked in by Nicholas Riley, 19 years ago

PSPreferencesController.[hm]: Filter hot keys. Read and write to
preferences.

NJRHotKeyField.[hm]: Added property list serialization of hot key data
(not quite like PSAlarm/Alert because we're a GUI element). Added
delegate method for hot key filtering and action triggering when hot
key set or cleared. Only use filtered modifiers to avoid bizarre
behavior. More bug fixes.

English.lproj/Preferences.nib: Connected Clear button. Connected
NJRHotKeyField delegate and target to PSPreferencesController.

NSString-NJRExtensions.[hm]: Replaced "...modifierMask" with
"...modifierFlags" because it's more appropriate.

File size: 3.7 KB
Line 
1//
2//  PSPreferencesController.m
3//  Pester
4//
5//  Created by Nicholas Riley on Sat Mar 29 2003.
6//  Copyright (c) 2003 Nicholas Riley. All rights reserved.
7//
8
9#import "PSPreferencesController.h"
10#import "NJRHotKeyField.h"
11
12@implementation PSPreferencesController
13
14#pragma mark interface updating
15
16- (void)update;
17{
18    // perform any interface propagation that needs to be done
19}
20
21#pragma mark preferences I/O
22
23- (void)readFromPrefs;
24{
25    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
26    [setAlarmHotKey setFromPropertyList: [defaults dictionaryForKey: @"Pester set alarm system-wide keyboard shortcut"]];
27}
28
29- (void)writeToPrefs;
30{
31    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
32    [defaults setObject: [setAlarmHotKey propertyListRepresentation] forKey: @"Pester set alarm system-wide keyboard shortcut"];
33    [defaults synchronize];
34}
35
36#pragma mark initialize-release
37
38- (id)init {
39    if ( (self = [super initWithWindowNibName: @"Preferences"]) != nil) {
40        [self window]; // connect outlets
41        [self readFromPrefs];
42        [self update];
43        // command
44        NSMutableCharacterSet *set = [[NSCharacterSet alphanumericCharacterSet] mutableCopy];
45        [set addCharactersInString: @"`-=[]/\\ "];
46        commandRejectSet = [set copy];
47        [set release];
48        // no modifiers, shift, option, option-shift
49        set = [[NSCharacterSet alphanumericCharacterSet] mutableCopy];
50        [set formUnionWithCharacterSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
51        [set formUnionWithCharacterSet: [NSCharacterSet punctuationCharacterSet]];
52        [set addCharactersInString: @"\t\r\e\x7f\x03\x19"]; // tab, CR, escape, delete, enter, backtab
53        [set addCharactersInRange: NSMakeRange(0xF700, 0x1FF)]; // reserved function key range
54        [set removeCharactersInRange: NSMakeRange(NSF1FunctionKey, 15)]; // F1-F15
55        textRejectSet = [set copy];
56        [set release];
57        // command-shift
58        commandShiftRejectSet = [[NSCharacterSet characterSetWithCharactersInString: @"ACFGHIPQS~? "] retain];
59        // command-option
60        commandOptionRejectSet = [[NSCharacterSet characterSetWithCharactersInString: @"DW\\-= "] retain];
61    }
62    return self;
63}
64
65- (void)dealloc;
66{
67    [textRejectSet release];
68    [commandRejectSet release];
69    [commandShiftRejectSet release];
70    [commandOptionRejectSet release];
71}
72
73#pragma mark actions
74
75- (IBAction)hotKeySet:(NJRHotKeyField *)sender;
76{
77    [self writeToPrefs];
78}
79
80@end
81
82@implementation PSPreferencesController (NJRHotKeyFieldDelegate)
83
84- (BOOL)hotKeyField:(NJRHotKeyField *)hotKeyField shouldAcceptCharacter:(unichar)keyChar modifierFlags:(unsigned)modifierFlags rejectionMessage:(NSString **)message;
85{
86    *message = nil;
87   
88    if (modifierFlags == 0 || modifierFlags == NSShiftKeyMask || modifierFlags == NSAlternateKeyMask || modifierFlags == (NSShiftKeyMask | NSAlternateKeyMask)) {
89        *message = modifierFlags == 0 ? @"key is reserved for typing text" :
90                                        @"key combination is reserved for typing text";
91        return ![textRejectSet characterIsMember: keyChar];
92    }
93    if (modifierFlags == NSCommandKeyMask) {
94        *message = @"key combination is reserved for application use";
95        return ![commandRejectSet characterIsMember: keyChar];
96    }
97    if (modifierFlags == (NSCommandKeyMask | NSShiftKeyMask)) {
98        *message = @"key combination is reserved for application use";
99        return ![commandShiftRejectSet characterIsMember: keyChar];
100    }
101    if (modifierFlags == (NSCommandKeyMask | NSAlternateKeyMask)) {
102        *message = @"key combination is reserved for Mac OS X use";
103        return ![commandOptionRejectSet characterIsMember: keyChar];
104    }
105    return YES;
106}
107
108@end
Note: See TracBrowser for help on using the repository browser.