/
MainController.m
198 lines (172 loc) · 5.08 KB
/
MainController.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
//
// MainController.m
// ASCIIConverter
//
// Created by zonble on 2009/07/22.
//
#import "MainController.h"
#import "NSImage+ASCII.h"
#import "ASCIIImageView.h"
#import "MainController+Toolbar.h"
#import "MainController+AppDelegate.h"
#define kWidthPreference @"kWidthPreference"
#define kHeightPreference @"kHeightPreference"
@implementation MainController
- (NSFont *)_defaultFont
{
return [NSFont fontWithName:@"Monaco" size:10.0];
}
- (void)awakeFromNib
{
NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier:@"toolbar"];
toolbar.delegate = self;
toolbar.autosavesConfiguration = YES;
toolbar.allowsUserCustomization = YES;
self.window.toolbar = toolbar;
NSApp.delegate = self;
[self.window center];
// [[self window] setDelegate:self];
NSInteger width = [[NSUserDefaults standardUserDefaults] integerForKey:kWidthPreference];
if (!width) {
width = 100;
}
if (width > 200) {
width = 200;
}
NSInteger height = [[NSUserDefaults standardUserDefaults] integerForKey:kHeightPreference];
if (!height) {
height = 40;
}
if (height > 200) {
height = 200;
}
_widthField.integerValue = width;
_heightField.integerValue = height;
_textView.textStorage.font = [self _defaultFont];
}
#pragma mark -
- (IBAction)convert:(id)sender
{
NSInteger width = _widthField.integerValue;
NSInteger height = _heightField.integerValue;
NSImage *image = _imageView.image;
NSString *asciiArt = [image asciiArtWithWidth:width height:height];
NSAttributedString *s = [[NSAttributedString alloc] initWithString:asciiArt attributes:@{
NSForegroundColorAttributeName: [NSColor textColor],
NSFontAttributeName: [self _defaultFont]
}];
[_textView.textStorage setAttributedString:s];
}
- (IBAction)changeWidth:(id)sender
{
NSInteger width = _widthField.integerValue;
if (width > 200) {
width = 200;
}
if (width) {
[[NSUserDefaults standardUserDefaults] setInteger:width forKey:kWidthPreference];
if (_imageView.image) {
[self convert:nil];
}
}
}
- (IBAction)changeHeight:(id)sender
{
NSInteger height = _heightField.integerValue;
if (height > 200) {
height = 200;
}
if (height) {
[[NSUserDefaults standardUserDefaults] setInteger:height forKey:kHeightPreference];
if ([_imageView image]) {
[self convert:nil];
}
}
}
- (void)openPanelDidEnd:(NSOpenPanel *)panel returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
if (returnCode != NSOKButton) {
return;
}
NSString *filename = [panel filename];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:filename];
if (!image) {
return;
}
_imageView.image = image;
}
- (IBAction)loadImage:(id)sender
{
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
openPanel.message = NSLocalizedString(@"The image file to convert:", @"");
openPanel.prompt = NSLocalizedString(@"Load...", @"");
NSArray *types = @[@"gif", @"jpg", @"jpeg", @"png", @"bmp", @"tif", @"tiff", @"psd", @"pdf", @"ai"];
openPanel.allowedFileTypes = types;
openPanel.allowsOtherFileTypes = NO;
openPanel.allowsMultipleSelection = NO;
[openPanel beginSheetForDirectory:nil file:nil types:types modalForWindow:[self window] modalDelegate:self didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) contextInfo:NULL];
}
- (void)saveTextToPath:(NSString *)path
{
NSString *text = _textView.textStorage.string;
NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding];
BOOL result = [data writeToFile:path atomically:YES];
if (!result) {
_currentFilePath = nil;
NSRunAlertPanel(NSLocalizedString(@"Failed to save the file!", @"The message in the save panel."), NSLocalizedString(@"Please try again.", @"The message in the save panel."), NSLocalizedString(@"OK", @""), nil, nil);
}
else {
if ([_currentFilePath isEqualToString:path]) {
return;
}
_currentFilePath = path;
}
}
- (void)savePanelDidEnd:(NSSavePanel *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
if (returnCode != NSOKButton) {
return;
}
NSString *filename = [sheet filename];
[self saveTextToPath:filename];
}
- (IBAction)save:(id)sender
{
if (_currentFilePath) {
[self saveTextToPath:_currentFilePath];
}
else {
[self saveAs:nil];
}
}
- (IBAction)saveAs:(id)sender
{
NSSavePanel *savePanel = [NSSavePanel savePanel];
savePanel.message = NSLocalizedString(@"Save the converted text to a plain text file:", @"");
savePanel.prompt = NSLocalizedString(@"Save", @"");
savePanel.allowedFileTypes = @[@"txt"];
savePanel.allowsOtherFileTypes = NO;
[savePanel beginSheetForDirectory:nil file:@"ascii.txt" modalForWindow:[self window] modalDelegate:self didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:) contextInfo:NULL];
}
- (IBAction)copy:(id)sender
{
id responder = self.window.firstResponder;
if ([responder respondsToSelector:@selector(copy:)]) {
[responder copy:sender];
}
}
- (IBAction)openZonbleBlogURL:(id)sender
{
NSURL *URL = [NSURL URLWithString:@"http://zonble.net/"];
[[NSWorkspace sharedWorkspace] openURL:URL];
}
#pragma mark -
- (void)imageViewImageDidChange:(ASCIIImageView *)imageView
{
[self convert:nil];
}
- (void)imageViewImageDidDoubleClick:(ASCIIImageView *)imageView
{
[self loadImage:nil];
}
@end