source: trunk/Cocoa/Pester/Source/NJRHotKeyField.m@ 563

Last change on this file since 563 was 355, checked in by Nicholas Riley, 17 years ago

English.lproj/MainMenu.nib: Modernize menu and alarm set dialog
layout. Use keyed archiving (10.2+) nib format.

Info-Pester.plist: Moved from old PBX project.

NJRFSObjectSelector.m: Bug fixes from code sent to Joey: remove
incorrect usage of tryToPerform:with:; fix logic error in menu
construction. Work around Cocoa's deciding that the menu font size
needs adjustment when it doesn't - so the menu font size now matches
the button font size, though the position is still off. Don't pop up
a menu if we're disabled. Use IconRefs for menu icons, though not
(yet) for the button icon.

NJRHistoryTrackingComboBox.m: Remove item height adjustment
workaround; it now makes the items too tall.

NJRHotKey.m: Add a missing [super dealloc] caught by current GCC.

NJRHotKeyField.m: Add a missing [super dealloc] caught by current GCC.

NJRHotKeyManager.m: Add a missing [super dealloc] caught by current
GCC.

NJRIntervalField.m: Fix some type errors.

NJRQTMediaPopUpButton.m: Replace SoundFileManager SPI usage, which
doesn't work in Leopard anyway, with manual enumeration of system
sounds. Start migration to QTKit. Use IconRefs for menu icons.

NJRReadMeController.m: Change source encoding to UTF-8.

NJRSoundManager.m: Fix a type error.

NJRVoicePopUpButton.m: Change source encoding to UTF-8.

NSMenuItem-NJRExtensions.[hm]: Code from ICeCoffEE to use IconRefs for
menu item icons.

PSAlarm.m: Change source encoding to UTF-8.

PSAlarms.m: Fix a signedness mismatch.

PSAlarmsController.m: Change source encoding to UTF-8.

PSAlarmSetController.m: Set keyboard focus after unchecking "Do
script:" and "Play" checkboxes.

PSAlerts.m: Add a missing [super dealloc] caught by current GCC. Fix
a memory leak in property list serialization.

PSPowerManager.[hm]: There's now API for scheduling wakeups; use it
(the old code asserted on startup). To fix: removing scheduled
wakeup. Fix a small type-checking error.

PSPreferencesController.m: Add a missing [super dealloc] caught by
current GCC.

PSScriptAlert.m: Change source encoding to UTF-8.

PSTimeDateEditor.m: Fix a tiny, and one-time, memory leak.

PSTimer.m: Update for new PSPowerManager API.

Pester.pbproj: Deleted; now supporting OS X 10.4+ (up from 10.1,
aiee.)

Pester.xcodeproj: Xcode 2.4+ project, upgraded targets, etc.

SoundFileManager.h: Deleted; this SPI no longer exists in Leopard and
possibly earlier.

File size: 5.7 KB
RevLine 
[129]1//
2// NJRHotKeyField.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 "NJRHotKeyField.h"
10#import "NJRHotKeyFieldCell.h"
[131]11#import "NJRHotKey.h"
[129]12#import "NSString-NJRExtensions.h"
13
[130]14static const NSRange zeroRange = {0, 0};
15static const unsigned int capturedModifierMask = (NSShiftKeyMask |
16 NSControlKeyMask |
17 NSAlternateKeyMask |
18 NSCommandKeyMask);
19
[129]20static NSParagraphStyle *leftAlignStyle = nil, *centerAlignStyle = nil;
21static NSDictionary *statusAttributes = nil;
22
[130]23@implementation NJRHotKeyField
24
[129]25+ (void)initialize;
26{
27 NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
28 [paraStyle setAlignment: NSLeftTextAlignment];
29 leftAlignStyle = [paraStyle copy];
30 [paraStyle setAlignment: NSCenterTextAlignment];
31 centerAlignStyle = [paraStyle copy];
32 [paraStyle release];
33
34 statusAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
35 [NSFont systemFontOfSize: [NSFont labelFontSize]], NSFontAttributeName,
36 centerAlignStyle, NSParagraphStyleAttributeName, nil];
37}
38
39- (void)_setUp;
40{
41 [self setAllowsEditingTextAttributes: YES];
42 [self setImportsGraphics: NO];
43 [self cell]->isa = [NJRHotKeyFieldCell class];
44}
45
[131]46#pragma mark initialize-release
47
[129]48- (id)initWithCoder:(NSCoder *)coder;
49{
50 if ( (self = [super initWithCoder: coder]) != nil) {
51 [self _setUp];
52 }
53 return self;
54}
55
56- (id)initWithFrame:(NSRect)frameRect;
57{
58 if ( (self = [super initWithFrame: frameRect]) != nil) {
59 [self _setUp];
60 }
61 return self;
62}
63
[131]64- (void)dealloc;
65{
66 [hotKey release];
[355]67 [super dealloc];
[131]68}
69
70#pragma mark interface updating
71
[132]72- (void)showStatus:(NSString *)error;
73{
74 [self setAttributedStringValue:
75 [[NSAttributedString alloc] initWithString: [NSString stringWithFormat: @"(%@)", error]
76 attributes: statusAttributes]];
77 [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(clearStatus) object: nil];
78 [self performSelector: @selector(clearStatus) withObject: nil afterDelay: 0.5];
79 [[self currentEditor] setSelectedRange: zeroRange];
80}
[129]81
82- (void)previewKeyEquivalentAttributedString:(NSAttributedString *)equivString;
83{
84 NSMutableAttributedString *previewString = [equivString mutableCopy];
85 [previewString addAttribute: NSParagraphStyleAttributeName value: leftAlignStyle range: NSMakeRange(0, [previewString length])];
86 [self setAttributedStringValue: previewString];
87 [[self currentEditor] setSelectedRange: zeroRange];
88 [previewString release];
89}
90
91- (void)showKeyEquivalentAttributedStringFinalized:(BOOL)finalized;
92{
[131]93 if ([hotKey characters] == nil) {
[129]94 [self showStatus: @"none assigned"];
95 return;
96 }
[131]97 NSMutableAttributedString *equivString = [[[hotKey characters] keyEquivalentAttributedStringWithModifierFlags: [hotKey modifierFlags]] mutableCopy];
[129]98 [equivString addAttribute: NSParagraphStyleAttributeName
99 value: (finalized ? centerAlignStyle : leftAlignStyle)
100 range: NSMakeRange(0, [equivString length])];
101 [self setAttributedStringValue: equivString];
102 [[self currentEditor] setSelectedRange: zeroRange];
103 [equivString release];
104}
105
[132]106- (void)clearStatus;
107{
108 if ([[[self attributedStringValue] attributesAtIndex: 0 effectiveRange: NULL] isEqualToDictionary: statusAttributes]) {
109 [self showKeyEquivalentAttributedStringFinalized: ([[NSApp currentEvent] modifierFlags] & capturedModifierMask) == 0];
110 }
111}
112
[131]113#pragma mark event handling
[129]114
115- (void)keyUp:(NSEvent *)theEvent;
116{
117 NSString *characters = [theEvent charactersIgnoringModifiers];
[130]118 int length = [characters length];
119 if (length > 1) {
[129]120 [self showStatus: @"please press only one non-modifier key"];
121 return;
122 }
[130]123 if (length == 1) {
124 unsigned modifierFlags = ([theEvent modifierFlags] & capturedModifierMask);
125 id delegate = [self delegate];
126 NSString *message = nil;
127 if (delegate != nil && ![delegate hotKeyField: self shouldAcceptCharacter: [characters characterAtIndex: 0] modifierFlags: modifierFlags rejectionMessage: &message]) {
128 [self showStatus: message != nil ? message : @"key is unavailable for use"];
129 } else {
[131]130 [self setHotKey: [NJRHotKey hotKeyWithCharacters: characters modifierFlags: modifierFlags keyCode: [theEvent keyCode]]];
[130]131 [NSApp sendAction: [self action] to: [self target] from: self];
132 [self showKeyEquivalentAttributedStringFinalized: ([theEvent modifierFlags] & capturedModifierMask) == 0];
133 }
134 }
[129]135}
136
137- (BOOL)performKeyEquivalent:(NSEvent *)theEvent;
138{
139 [self keyUp: theEvent];
140 return [super performKeyEquivalent: theEvent];
141}
142
143- (void)flagsChanged:(NSEvent *)theEvent;
144{
145 unsigned modifierFlags = [theEvent modifierFlags];
146
[130]147 if ((modifierFlags & capturedModifierMask) == 0) {
[129]148 [self showKeyEquivalentAttributedStringFinalized: YES];
149 } else {
150 [self previewKeyEquivalentAttributedString:
[130]151 [@"" keyEquivalentAttributedStringWithModifierFlags: modifierFlags]];
[129]152 }
153}
154
[131]155#pragma mark acccessing
156
157- (NJRHotKey *)hotKey;
[129]158{
[131]159 return hotKey;
[129]160}
161
[131]162- (void)setHotKey:(NJRHotKey *)aKey;
[130]163{
[131]164 if (aKey != hotKey) {
165 [hotKey release];
166 hotKey = [aKey retain];
167 [self showKeyEquivalentAttributedStringFinalized: ([[NSApp currentEvent] modifierFlags] & capturedModifierMask) == 0];
168 }
[130]169}
170
[131]171#pragma mark actions
172
173- (IBAction)clear:(id)sender;
[130]174{
[131]175 [self setHotKey: nil];
176 [NSApp sendAction: [self action] to: [self target] from: self];
177 [self showKeyEquivalentAttributedStringFinalized: YES];
[130]178}
179
[129]180@end
Note: See TracBrowser for help on using the repository browser.