[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"
|
---|
| 11 | #import "NSString-NJRExtensions.h"
|
---|
| 12 |
|
---|
| 13 | @implementation NJRHotKeyField
|
---|
| 14 |
|
---|
| 15 | const NSRange zeroRange = {0, 0};
|
---|
| 16 | static NSParagraphStyle *leftAlignStyle = nil, *centerAlignStyle = nil;
|
---|
| 17 | static NSDictionary *statusAttributes = nil;
|
---|
| 18 |
|
---|
| 19 | + (void)initialize;
|
---|
| 20 | {
|
---|
| 21 | NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
|
---|
| 22 | [paraStyle setAlignment: NSLeftTextAlignment];
|
---|
| 23 | leftAlignStyle = [paraStyle copy];
|
---|
| 24 | [paraStyle setAlignment: NSCenterTextAlignment];
|
---|
| 25 | centerAlignStyle = [paraStyle copy];
|
---|
| 26 | [paraStyle release];
|
---|
| 27 |
|
---|
| 28 | statusAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
|
---|
| 29 | [NSFont systemFontOfSize: [NSFont labelFontSize]], NSFontAttributeName,
|
---|
| 30 | centerAlignStyle, NSParagraphStyleAttributeName, nil];
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | - (void)_setUp;
|
---|
| 34 | {
|
---|
| 35 | [self setAllowsEditingTextAttributes: YES];
|
---|
| 36 | [self setImportsGraphics: NO];
|
---|
| 37 | [self cell]->isa = [NJRHotKeyFieldCell class];
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | - (id)initWithCoder:(NSCoder *)coder;
|
---|
| 41 | {
|
---|
| 42 | if ( (self = [super initWithCoder: coder]) != nil) {
|
---|
| 43 | [self _setUp];
|
---|
| 44 | }
|
---|
| 45 | return self;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | - (id)initWithFrame:(NSRect)frameRect;
|
---|
| 49 | {
|
---|
| 50 | if ( (self = [super initWithFrame: frameRect]) != nil) {
|
---|
| 51 | [self _setUp];
|
---|
| 52 | }
|
---|
| 53 | return self;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | // XXX still problems with command-A the first time
|
---|
| 57 |
|
---|
| 58 | - (void)previewKeyEquivalentAttributedString:(NSAttributedString *)equivString;
|
---|
| 59 | {
|
---|
| 60 | NSMutableAttributedString *previewString = [equivString mutableCopy];
|
---|
| 61 | [previewString addAttribute: NSParagraphStyleAttributeName value: leftAlignStyle range: NSMakeRange(0, [previewString length])];
|
---|
| 62 | [self setAttributedStringValue: previewString];
|
---|
| 63 | [[self currentEditor] setSelectedRange: zeroRange];
|
---|
| 64 | [previewString release];
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | - (void)showStatus:(NSString *)error;
|
---|
| 68 | {
|
---|
| 69 | [self setAttributedStringValue:
|
---|
| 70 | [[NSAttributedString alloc] initWithString: [NSString stringWithFormat: @"(%@)", error]
|
---|
| 71 | attributes: statusAttributes]];
|
---|
| 72 | [[self currentEditor] setSelectedRange: zeroRange];
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | - (void)showKeyEquivalentAttributedStringFinalized:(BOOL)finalized;
|
---|
| 76 | {
|
---|
| 77 | if (hotKeyCharacters == nil) {
|
---|
| 78 | [self showStatus: @"none assigned"];
|
---|
| 79 | return;
|
---|
| 80 | }
|
---|
| 81 | NSMutableAttributedString *equivString = [[hotKeyCharacters keyEquivalentAttributedStringWithModifierMask: hotKeyModifierFlags] mutableCopy];
|
---|
| 82 | [equivString addAttribute: NSParagraphStyleAttributeName
|
---|
| 83 | value: (finalized ? centerAlignStyle : leftAlignStyle)
|
---|
| 84 | range: NSMakeRange(0, [equivString length])];
|
---|
| 85 | [self setAttributedStringValue: equivString];
|
---|
| 86 | [[self currentEditor] setSelectedRange: zeroRange];
|
---|
| 87 | [equivString release];
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | - (void)setHotKeyEvent:(NSEvent *)theEvent;
|
---|
| 91 | {
|
---|
| 92 | [hotKeyCharacters release]; hotKeyCharacters = [[theEvent charactersIgnoringModifiers] retain];
|
---|
| 93 | hotKeyModifierFlags = [theEvent modifierFlags];
|
---|
| 94 | hotKeyCode = [theEvent keyCode];
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | - (void)keyUp:(NSEvent *)theEvent;
|
---|
| 98 | {
|
---|
| 99 | NSString *characters = [theEvent charactersIgnoringModifiers];
|
---|
| 100 | if ([characters length] > 1) {
|
---|
| 101 | [self showStatus: @"please press only one non-modifier key"];
|
---|
| 102 | return;
|
---|
| 103 | }
|
---|
| 104 | // Yay, we can ask for keyCode, which is likely to be the same as Carbon's, I hope.
|
---|
| 105 | [self setHotKeyEvent: theEvent];
|
---|
| 106 |
|
---|
| 107 | [self showKeyEquivalentAttributedStringFinalized: ([theEvent modifierFlags] == 0)];
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | - (BOOL)performKeyEquivalent:(NSEvent *)theEvent;
|
---|
| 111 | {
|
---|
| 112 | [self keyUp: theEvent];
|
---|
| 113 | return [super performKeyEquivalent: theEvent];
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | - (void)flagsChanged:(NSEvent *)theEvent;
|
---|
| 117 | {
|
---|
| 118 | unsigned modifierFlags = [theEvent modifierFlags];
|
---|
| 119 |
|
---|
| 120 | // XXX why does my API call it a modifier mask when NSEvent's API calls it modifier flags? Check HostLauncher for usage.
|
---|
| 121 |
|
---|
| 122 | if (modifierFlags == 0) {
|
---|
| 123 | [self showKeyEquivalentAttributedStringFinalized: YES];
|
---|
| 124 | } else {
|
---|
| 125 | [self previewKeyEquivalentAttributedString:
|
---|
| 126 | [@"" keyEquivalentAttributedStringWithModifierMask: modifierFlags]];
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | - (IBAction)clear:(id)sender;
|
---|
| 131 | {
|
---|
| 132 | [hotKeyCharacters release]; hotKeyCharacters = 0;
|
---|
| 133 | hotKeyModifierFlags = 0;
|
---|
| 134 | hotKeyCode = 0;
|
---|
| 135 | [self showKeyEquivalentAttributedStringFinalized: YES];
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | @end
|
---|