forked from veryweblog/ichm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CHMTextEncodingMenu.m
89 lines (79 loc) · 1.93 KB
/
CHMTextEncodingMenu.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
//
// CHMTextEncodingMenu.m
// ichm
//
// Created by Robin Lu on 8/1/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "CHMTextEncodingMenu.h"
#import "CHMDocument.h"
@implementation CHMTextEncodingMenu
- (id)init
{
initialized = NO;
encodingNames = [[NSMutableArray alloc] init];
return self;
}
- (void) dealloc
{
[encodingNames release];
[super dealloc];
}
- (void)menuNeedsUpdate:(NSMenu *)menu
{
if(!initialized)
[self initEncodingMenu];
NSDocumentController * controller = [NSDocumentController sharedDocumentController];
CHMDocument *doc = [controller currentDocument];
[doc setupEncodingMenu];
}
- (void)initEncodingMenu
{
if(initialized)
return;
NSString *path = [[NSBundle mainBundle] pathForResource:@"textencoding" ofType:@"plist"];
NSData *plistData;
NSString *error;
NSPropertyListFormat format;
NSArray * plist;
plistData = [NSData dataWithContentsOfFile:path];
plist = [NSPropertyListSerialization propertyListFromData:plistData
mutabilityOption:NSPropertyListImmutable
format:&format
errorDescription:&error];
if(!plist)
{
NSLog(error);
[error release];
return;
}
NSMenu * submenu = [menu submenu];
NSInteger lastitem;
for(NSArray *section in plist)
{
for(NSDictionary *item in section)
{
NSString *title = [item objectForKey:@"title"];
NSMenuItem *newitem = [[NSMenuItem alloc] init];
[newitem setTitle:title];
int tag = [encodingNames count];
NSString *name = [item objectForKey:@"name"];
[encodingNames addObject:name];
[newitem setTag:tag];
[submenu addItem:newitem];
[newitem autorelease];
}
NSMenuItem *seperator = [NSMenuItem separatorItem];
[submenu addItem:seperator];
lastitem = [submenu indexOfItem:seperator];
}
[submenu removeItemAtIndex:lastitem];
initialized = YES;
}
- (NSString*)getEncodingByTag:(int)tag
{
if(0==tag)
return nil;
return [encodingNames objectAtIndex:tag];
}
@end