forked from KOed/welly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WLEmoticonsPanelController.m
146 lines (121 loc) · 3.73 KB
/
WLEmoticonsPanelController.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
//
// WLEmoticonDelegate.m
// Welly
//
// Created by K.O.ed on 09-9-27.
// Copyright 2009 Welly Group. All rights reserved.
//
#import "WLEmoticonsPanelController.h"
#import "YLEmoticon.h"
#import "SynthesizeSingleton.h"
#define kEmoticonPanelNibFilename @"EmoticonsPanel"
@interface WLEmoticonsPanelController ()
- (void)loadNibFile;
- (void)loadEmoticons;
- (void)saveEmoticons;
// emoticons accessors
- (void)addEmoticon:(YLEmoticon *)emoticon;
- (unsigned)countOfEmoticons;
- (id)objectInEmoticonsAtIndex:(unsigned)theIndex;
- (void)getEmoticons:(id *)objsPtr
range:(NSRange)range;
- (void)insertObject:(id)obj
inEmoticonsAtIndex:(unsigned)theIndex;
- (void)removeObjectFromEmoticonsAtIndex:(unsigned)theIndex;
- (void)replaceObjectInEmoticonsAtIndex:(unsigned)theIndex withObject:(id)obj;
@end
@implementation WLEmoticonsPanelController
@synthesize emoticons = _emoticons;
SYNTHESIZE_SINGLETON_FOR_CLASS(WLEmoticonsPanelController);
- (id)init {
if (self = [super init]) {
@synchronized(self) {
if (!_emoticons)
_emoticons = [[NSMutableArray alloc] init];
[self loadNibFile];
}
}
return self;
}
- (void)dealloc {
[_emoticons release];
[super dealloc];
}
- (void)loadNibFile {
if (_emoticonsPanel) {
// Already loaded, return quietly
return;
}
// Load Nib file and load all emoticons in
if ([NSBundle loadNibNamed:kEmoticonPanelNibFilename owner:self]) {
[self loadEmoticons];
}
}
#pragma mark -
#pragma mark IBActions
- (void)openEmoticonsPanel {
// Load Nib file if necessary
[self loadNibFile];
[_emoticonsPanel makeKeyAndOrderFront:self];
}
- (IBAction)closeEmoticonsPanel:(id)sender {
[_emoticonsPanel endEditingFor:nil];
[_emoticonsPanel makeFirstResponder:_emoticonsPanel];
[_emoticonsPanel orderOut:self];
[self saveEmoticons];
}
- (IBAction)inputSelectedEmoticon:(id)sender {
[self closeEmoticonsPanel:sender];
if ([[[NSApp keyWindow] firstResponder] conformsToProtocol:@protocol(NSTextInput)]) {
id <NSTextInput> textInput = (id <NSTextInput>)[[NSApp keyWindow] firstResponder];
NSArray *a = [_emoticonsController selectedObjects];
if ([a count] == 1) {
YLEmoticon *e = [a objectAtIndex:0];
[textInput insertText:[e content]];
}
}
}
#pragma mark -
#pragma mark Save/Load Emoticons
- (void)loadEmoticons {
NSArray *a = [[NSUserDefaults standardUserDefaults] arrayForKey:@"Emoticons"];
for (NSDictionary *d in a)
[self addEmoticon:[YLEmoticon emoticonWithDictionary:d]];
}
- (void)saveEmoticons {
NSMutableArray *a = [NSMutableArray array];
for (YLEmoticon *e in _emoticons)
[a addObject:[e dictionaryOfEmoticon]];
[[NSUserDefaults standardUserDefaults] setObject:a forKey:@"Emoticons"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
#pragma mark -
#pragma mark Emoticons Accessors
- (unsigned)countOfEmoticons {
return [_emoticons count];
}
- (id)objectInEmoticonsAtIndex:(unsigned)theIndex {
return [_emoticons objectAtIndex:theIndex];
}
- (void)getEmoticons:(id *)objsPtr
range:(NSRange)range {
[_emoticons getObjects:objsPtr range:range];
}
- (void)insertObject:(id)obj
inEmoticonsAtIndex:(unsigned)theIndex {
[_emoticons insertObject:obj atIndex:theIndex];
}
- (void)removeObjectFromEmoticonsAtIndex:(unsigned)theIndex {
[_emoticons removeObjectAtIndex:theIndex];
}
- (void)replaceObjectInEmoticonsAtIndex:(unsigned)theIndex withObject:(id)obj {
[_emoticons replaceObjectAtIndex:theIndex withObject:obj];
}
- (void)addEmoticon:(YLEmoticon *)emoticon {
[self insertObject:emoticon inEmoticonsAtIndex:[self countOfEmoticons]];
}
- (void)addEmoticonFromString:(NSString *)string {
YLEmoticon *emoticon = [YLEmoticon emoticonWithString:string];
[self addEmoticon:emoticon];
}
@end