[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] | 14 | static const NSRange zeroRange = {0, 0};
|
---|
| 15 | static const unsigned int capturedModifierMask = (NSShiftKeyMask |
|
---|
| 16 | NSControlKeyMask |
|
---|
| 17 | NSAlternateKeyMask |
|
---|
| 18 | NSCommandKeyMask);
|
---|
| 19 |
|
---|
[129] | 20 | static NSParagraphStyle *leftAlignStyle = nil, *centerAlignStyle = nil;
|
---|
| 21 | static 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
|
---|