source: releases/Pester/1.1b7/Source/NSString-NJRExtensions.m@ 541

Last change on this file since 541 was 130, checked in by Nicholas Riley, 22 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: 6.9 KB
Line 
1//
2// NSString-NJRExtensions.m
3// Pester
4//
5// Created by Nicholas Riley on Mon Dec 16 2002.
6// Copyright (c) 2002 Nicholas Riley. All rights reserved.
7//
8
9#import "NSString-NJRExtensions.h"
10#import "NSFont-NJRExtensions.h"
11#include <Carbon/Carbon.h>
12
13@implementation NSString (NJRExtensions)
14
15+ (NSString *)stringWithCharacter:(unichar)character;
16{
17 return [self stringWithCharacters: &character length: 1];
18}
19
20- (NSAttributedString *)attributedStringWithFont:(NSFont *)font;
21{
22 return [[[NSAttributedString alloc] initWithString: self attributes: [NSDictionary dictionaryWithObject: font forKey: NSFontAttributeName]] autorelease];
23}
24
25- (NSAttributedString *)underlined;
26{
27 return [[[NSAttributedString alloc] initWithString: self attributes: [NSDictionary dictionaryWithObject: [NSNumber numberWithInt: NSSingleUnderlineStyle] forKey: NSUnderlineStyleAttributeName]] autorelease];
28}
29
30- (NSAttributedString *)small;
31{
32 return [self attributedStringWithFont: [NSFont systemFontOfSize: [NSFont smallSystemFontSize]]];
33}
34
35- (NSAttributedString *)smallBold;
36{
37 return [self attributedStringWithFont: [NSFont boldSystemFontOfSize: [NSFont smallSystemFontSize]]];
38}
39
40static unichar combiningHelpChar[] = {0x003F, 0x20DD};
41
42static NSFont *menuItemCmdKeyFont = nil;
43static NSFont *menuItemFont = nil;
44
45static void initialize() {
46 if (menuItemCmdKeyFont != nil) return;
47
48 menuItemCmdKeyFont = [[NSFont themeFont: kThemeMenuItemCmdKeyFont] retain];
49 menuItemFont = [[NSFont themeFont: kThemeMenuItemFont] retain];
50}
51
52- (NSString *)keyEquivalentString;
53{
54 if ([self length] != 0) {
55 const char *str = [self UTF8String];
56 unichar keyChar;
57 if (str[1] != '\0') {
58 keyChar = [self characterAtIndex: 0];
59 switch (keyChar) {
60 case NSUpArrowFunctionKey: keyChar = 0x21E1; break;
61 case NSDownArrowFunctionKey: keyChar = 0x21E3; break;
62 case NSLeftArrowFunctionKey: keyChar = 0x21E0; break;
63 case NSRightArrowFunctionKey: keyChar = 0x21E2; break;
64 case NSInsertFunctionKey:
65 return [NSString stringWithCharacters: combiningHelpChar length: 2];
66 case NSDeleteFunctionKey: keyChar = 0x2326; break;
67 case NSHomeFunctionKey: keyChar = 0x2196; break;
68 case NSEndFunctionKey: keyChar = 0x2198; break;
69 case NSPageUpFunctionKey: keyChar = 0x21DE; break;
70 case NSPageDownFunctionKey: keyChar = 0x21DF; break;
71 case NSClearLineFunctionKey: keyChar = 0x2327; break;
72 default:
73 if (keyChar >= NSF1FunctionKey && keyChar <= NSF35FunctionKey) {
74 return [NSString stringWithFormat: @"F%u", keyChar - NSF1FunctionKey + 1];
75 }
76 return [NSString stringWithFormat: @"[unknown %lX]", keyChar];
77 }
78 } else if (str[0] >= 'A' && str[0] <= 'Z') {
79 return self;
80 } else if (str[0] >= 'a' && str[0] <= 'z') return [self uppercaseString];
81 else switch (str[0]) {
82 case '\t': keyChar = 0x21e5; break;
83 case '\r': keyChar = 0x21a9; break;
84 case '\e': keyChar = 0x238b; break;
85 case ' ': keyChar = 0x2423; break;
86 case 0x7f: keyChar = 0x232b; break; // delete
87 case 0x03: keyChar = 0x2324; break; // enter
88 case 0x19: keyChar = 0x21e4; break; // backtab
89 case 0: return @"";
90 // case '': keyChar = 0x; break;
91 default: return self; // return [NSString stringWithFormat: @"[huh? %x]", (int)str[0]]; //
92 }
93 return [NSString stringWithCharacter: keyChar];
94 }
95 return self;
96}
97
98- (NSAttributedString *)keyEquivalentAttributedStringWithModifierFlags:(unsigned int)modifierFlags;
99{
100 initialize();
101 NSString *keyEquivalentStringNoMask = [self keyEquivalentString];
102 NSAttributedString *keyEquivalentAttributedString =
103 [[NSString stringWithFormat: @"%@%@%@%@%@",
104 (modifierFlags & NSControlKeyMask) ? [NSString stringWithCharacter: kControlUnicode] : @"",
105 (modifierFlags & NSAlternateKeyMask) ? [NSString stringWithCharacter: kOptionUnicode] : @"",
106 (modifierFlags & NSShiftKeyMask) ? [NSString stringWithCharacter: kShiftUnicode] : @"",
107 (modifierFlags & NSCommandKeyMask) ? [NSString stringWithCharacter: kCommandUnicode] : @"",
108 keyEquivalentStringNoMask]
109 attributedStringWithFont: menuItemCmdKeyFont];
110 unsigned noMaskLength = [keyEquivalentStringNoMask length];
111 if (noMaskLength > 3 || // Fxx
112 (noMaskLength == 1 && [keyEquivalentStringNoMask characterAtIndex: 0] <= 0x7F)) {
113 NSMutableAttributedString *astr = [keyEquivalentAttributedString mutableCopy];
114 [astr setAttributes: [NSDictionary dictionaryWithObject: menuItemFont forKey: NSFontAttributeName] range: NSMakeRange([astr length] - noMaskLength, noMaskLength)];
115 keyEquivalentAttributedString = [[astr copy] autorelease];
116 [astr release];
117 }
118 return keyEquivalentAttributedString;
119}
120
121+ (NSString *)ellipsisString;
122{
123 static NSString *ellipsis = nil;
124 if (ellipsis == nil) {
125 const unichar ellipsisChar = 0x2026;
126 ellipsis = [[NSString alloc] initWithCharacters: &ellipsisChar length: 1];
127 }
128 return ellipsis;
129}
130
131@end
132
133@implementation NSMutableString (NJRExtensions)
134
135- (void)truncateToLength:(unsigned)maxLength by:(NSLineBreakMode)method;
136{
137 if ([self length] > maxLength) {
138 NSRange range = {0, [self length] - maxLength};
139 switch (method) {
140 case NSLineBreakByTruncatingHead:
141 range.location = 0;
142 break;
143 case NSLineBreakByTruncatingMiddle:
144 range.location = maxLength / 2;
145 break;
146 case NSLineBreakByTruncatingTail:
147 range.location = maxLength;
148 break;
149 default:
150 range.location = maxLength;
151 break;
152 }
153 [self replaceCharactersInRange: range withString: [NSString ellipsisString]];
154 }
155}
156
157- (void)truncateToWidth:(float)maxWidth by:(NSLineBreakMode)method withAttributes:(NSDictionary *)attributes;
158{
159 if ([self sizeWithAttributes: attributes].width > maxWidth) {
160 float width = maxWidth;
161 int min = 0, max = [self length], avg;
162 NSMutableString *original = [self mutableCopy];
163 while (max >= min) {
164 avg = (max + min) / 2;
165 [self truncateToLength: avg by: method];
166 width = [self sizeWithAttributes: attributes].width;
167 if (width > maxWidth) {
168 max = avg - 1; // too wide
169 } else if (width == maxWidth) {
170 break;
171 } else {
172 min = avg + 1; // too narrow
173 [self setString: original];
174 }
175 }
176 if (width != maxWidth)
177 [self truncateToLength: max by: method];
178 [original release];
179 }
180}
181
182@end
Note: See TracBrowser for help on using the repository browser.