-
Notifications
You must be signed in to change notification settings - Fork 0
/
TUEncodingPopUp.m
224 lines (188 loc) · 8.52 KB
/
TUEncodingPopUp.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// TUEncodingPopUp.m
// The Unarchiver
//
// Created by Dag Ågren on 2006-07-08.
// Copyright 2006 __MyCompanyName__. All rights reserved.
//
#import "TUEncodingPopUp.h"
@implementation TUEncodingPopUp
-(id)initWithFrame:(NSRect)frame
{
if(self=[super initWithFrame:frame])
{
//[self buildEncodingList];
}
return self;
}
-(id)initWithCoder:(NSCoder *)coder
{
if(self=[super initWithCoder:coder])
{
//[self buildEncodingList];
}
return self;
}
-(void)buildEncodingList
{
[self buildEncodingListMatchingData:nil];
}
-(void)buildEncodingListWithAutoDetect
{
[self buildEncodingListMatchingData:nil];
[[self menu] addItem:[NSMenuItem separatorItem]];
NSMenuItem *item=[[NSMenuItem alloc] init];
[item setTitle:NSLocalizedString(@"Detect automatically",@"Option in the encoding pop-up to detect the encoding automatically")];
[item setTag:0];
[[self menu] addItem:item];
[item release];
}
-(void)buildEncodingListMatchingData:(NSData *)data
{
[self removeAllItems];
NSMutableDictionary *normalattrs,*smallattrs;
if(data)
{
normalattrs=[NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSFont menuFontOfSize:[NSFont systemFontSize]],NSFontAttributeName,
nil];
smallattrs=[NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSFont menuFontOfSize:[NSFont smallSystemFontSize]],NSFontAttributeName,
nil];
float maxwidth=[[self class] maximumEncodingNameWidthWithAttributes:normalattrs];
NSMutableParagraphStyle *parastyle=[[NSMutableParagraphStyle new] autorelease];
[parastyle setTabStops:[NSArray arrayWithObjects:
[[[NSTextTab alloc] initWithType:NSLeftTabStopType location:maxwidth+10] autorelease],
nil]];
[normalattrs setObject:parastyle forKey:NSParagraphStyleAttributeName];
[smallattrs setObject:parastyle forKey:NSParagraphStyleAttributeName];
}
NSArray *encodings=[[self class] encodings];
NSEnumerator *enumerator=[encodings objectEnumerator];
NSDictionary *encdict;
while(encdict=[enumerator nextObject])
{
NSStringEncoding encoding=[[encdict objectForKey:@"Encoding"] longValue];
NSString *str=nil;
if(data)
{
str=[[NSString alloc] initWithData:data encoding:encoding];
if(!str) continue;
}
NSString *encname=[encdict objectForKey:@"Name"];
NSMenuItem *item=[[NSMenuItem alloc] init];
if(str)
{
NSMutableAttributedString *attrstr=[[[NSMutableAttributedString alloc]
initWithString:[NSString stringWithFormat:@"%@\t%C %@",encname,0x27a4,str]
attributes:normalattrs] autorelease];
[attrstr setAttributes:smallattrs range:NSMakeRange([encname length],[str length]+3)];
[item setAttributedTitle:attrstr];
}
else
{
[item setTitle:encname];
}
[item setTag:encoding];
[[self menu] addItem:item];
[item release];
[str release];
}
}
NSComparisonResult encoding_sort(NSDictionary *enc1,NSDictionary *enc2,void *dummy)
{
NSString *name1=[enc1 objectForKey:@"Name"];
NSString *name2=[enc2 objectForKey:@"Name"];
/*BOOL isunicode1=[name1 hasPrefix:@"Unicode"];
BOOL isunicode2=[name2 hasPrefix:@"Unicode"];
if(isunicode1&&!isunicode2) return NSOrderedAscending;
else if(!isunicode1&&isunicode2) return NSOrderedDescending;
else*/ return [name1 compare:name2 options:NSCaseInsensitiveSearch|NSNumericSearch];
}
+(NSArray *)encodings
{
NSMutableArray *encodingarray=[NSMutableArray array];
const CFStringEncoding *encodings=CFStringGetListOfAvailableEncodings();
while(*encodings!=kCFStringEncodingInvalidId)
{
CFStringEncoding cfencoding=*encodings++;
NSString *name=[self nameOfEncoding:cfencoding];
NSStringEncoding encoding=CFStringConvertEncodingToNSStringEncoding(cfencoding);
if(!name) continue;
if(encoding==10) continue;
[encodingarray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
name,@"Name",
[NSNumber numberWithLong:encoding],@"Encoding",
nil]];
}
return [encodingarray sortedArrayUsingFunction:encoding_sort context:nil];
}
+(NSString *)nameOfEncoding:(CFStringEncoding)encoding
{
UInt8 str[256];
ByteCount namelen;
TextEncoding nameenc;
if(GetTextEncodingName(encoding,kTextEncodingFullName,
GetScriptManagerVariable(smRegionCode),kCFStringEncodingUTF8,
256,&namelen,NULL,&nameenc,str)!=noErr) return nil;
// return [[[NSString alloc] initWithBytes:str length:namelen encoding:CFStringConvertEncodingToNSStringEncoding(nameenc)] autorelease];
return [[[NSString alloc] initWithBytes:str length:namelen encoding:NSUTF8StringEncoding] autorelease];
}
+(float)maximumEncodingNameWidthWithAttributes:(NSDictionary *)attrs
{
float maxwidth=0;
NSArray *encodings=[[self class] encodings];
NSEnumerator *enumerator=[encodings objectEnumerator];
NSDictionary *encdict;
while(encdict=[enumerator nextObject])
{
NSString *name=[encdict objectForKey:@"Name"];
float width=[name sizeWithAttributes:attrs].width;
if(width>maxwidth) maxwidth=width;
}
return maxwidth;
}
@end
/*+(NSDictionary *)encodingDictionary
{
static NSDictionary *encodingdict=nil;
if(!encodingdict) encodingdict=[[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithUnsignedInt:NSASCIIStringEncoding],@"US-ASCII",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingBig5)],@"Big5",
[NSNumber numberWithUnsignedInt:NSJapaneseEUCStringEncoding],@"EUC-JP",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingEUC_KR)],@"EUC-KR",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)],@"GB18030",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_2312_80)],@"GB2312",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingHZ_GB_2312)],@"HZ-GB-2312",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingDOSCyrillic)],@"IBM855",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingDOSRussian)],@"IBM866",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingISO_2022_CN)],@"ISO-2022-CN",
[NSNumber numberWithUnsignedInt:NSISO2022JPStringEncoding],@"ISO-2022-JP",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingISO_2022_KR)],@"ISO-2022-KR",
[NSNumber numberWithUnsignedInt:NSISOLatin2StringEncoding],@"ISO-8859-2",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingISOLatinCyrillic)],@"ISO-8859-5",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingISOLatinGreek)],@"ISO-8859-7",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingISOLatinHebrew)],@"ISO-8859-8",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingISOLatinHebrew)],@"ISO-8859-8-I", // not sure about this!
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingKOI8_R)],@"KOI8-R",
[NSNumber numberWithUnsignedInt:NSShiftJISStringEncoding],@"Shift_JIS",
// TIS-620 - missing
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF16BE)],@"UTF-16BE",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF16LE)],@"UTF-16LE",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF32BE)],@"UTF-32BE",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF32LE)],@"UTF-32LE",
[NSNumber numberWithUnsignedInt:NSUTF8StringEncoding],@"UTF-8",
// X-ISO-10646-UCS-4-2143 - missing
// X-ISO-10646-UCS-4-3412 - missing
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)],@"gb18030",
[NSNumber numberWithUnsignedInt:NSWindowsCP1250StringEncoding],@"windows-1250",
[NSNumber numberWithUnsignedInt:NSWindowsCP1251StringEncoding],@"windows-1251",
[NSNumber numberWithUnsignedInt:NSWindowsCP1252StringEncoding],@"windows-1252",
[NSNumber numberWithUnsignedInt:NSWindowsCP1253StringEncoding],@"windows-1253",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingWindowsHebrew)],@"windows-1255",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingEUC_TW)],@"x-euc-tw",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingMacCyrillic)],@"x-mac-cyrillic",
[NSNumber numberWithUnsignedInt:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingMacHebrew)],@"x-mac-hebrew",
nil];
return encodingdict;
}*/