-
Notifications
You must be signed in to change notification settings - Fork 2
/
NSMainMenuController.m
119 lines (104 loc) · 3.34 KB
/
NSMainMenuController.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
//
// NSMainMenuController.m
// iConMerge
//
// Created by Daniel Leping on 2/1/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "NSAnyImageView.h"
#import "NSSaveableImageView.h"
#import "NSMainMenuController.h"
#import "IconFamily.h"
@implementation NSMainMenuController
@synthesize lastOpenedImageView=_lastOpenedImageView;
- (void)awakeFromNib {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setLastOpenedImage:) name:NSAnyImageViewDraggedImage object:nil];
}
- (IBAction) swapAction:sender {
NSImage* image = imageView1.image;
imageView1.image = imageView2.image;
imageView2.image = image;
[self mergeAction:sender];
}
- (IBAction) mergeAction:sender {
int part = 2;
int rate = 5;
NSImage* image1 = [imageView1 image];
NSImage* image2 = [imageView2 image];
NSSize size1 = [image1 size];
NSSize size2 = [image2 size];
NSSize size1quater = { size1.width / rate, size1.height / rate };
NSImage* resultImage = [[NSImage alloc] initWithSize:size1];
NSRect rect1from = { 0, 0, size1.width, size1.height };
NSRect rect1to = rect1from;
NSRect rect2from = { 0, 0, size2.width, size2.height };
NSRect rect2to = { size1quater.width * (rate - part), 0, size1quater.width * part, size1quater.height * part };
[resultImage lockFocus];
[image1 drawInRect:rect1to fromRect:rect1from operation:NSCompositeSourceOver fraction:1];
[image2 drawInRect:rect2to fromRect:rect2from operation:NSCompositeSourceOver fraction:1];
[resultImage unlockFocus];
imageView3.image = resultImage;
[resultImage release];
}
- (IBAction) openAction:sender {
NSOpenPanel* panel = [NSOpenPanel openPanel];
if( [panel runModalForTypes:nil] == NSOKButton ) {
BOOL success = FALSE;
if (self.lastOpenedImageView == imageView1) {
success = [imageView2 loadFile:[panel filename]];
//self.lastOpenedImageView = imageView2;
} else {
success = [imageView1 loadFile:[panel filename]];
//self.lastOpenedImageView = imageView1;
}
if(success) {
[self mergeAction:sender];
}
}
}
- (void)setLastOpenedImage:(id)sender
{
self.lastOpenedImageView = [sender object];
[self mergeAction:sender];
}
- (IBAction) saveAction:sender {
BOOL result;
NSString* filename;
if([sender tag] == SaveItem) {
NSSavePanel* panel = [NSSavePanel savePanel];
[panel setRequiredFileType:@"icns"];
if( result = [panel runModal] == NSOKButton ) {
filename = [panel filename];
}
} else {
NSOpenPanel* panel = [NSOpenPanel openPanel];
[panel setPrompt:@"Save"];
[panel setTitle:@"Save"];
if( result = [panel runModal] == NSOKButton ) {
filename = [panel filename];
}
}
if( result ) {
NSImage* image = [imageView3 image];
if([sender tag] == SaveItem) {
NSData* data = [image TIFFRepresentation];
IconFamily* iconFamily = [IconFamily iconFamilyWithThumbnailsOfImage:[[NSImage alloc] initWithData:data]];
result = [iconFamily writeToFile:filename];
} else {
result = [[NSWorkspace sharedWorkspace] setIcon:image forFile:filename options:0];
}
if( !result ) {
NSRunAlertPanel(@"File Writing Error", [NSString stringWithFormat:@"Sorry, but I failed to save the icon at \"%@\"", filename], nil, nil, nil);
}
}
}
- (IBAction) windowClose:sender {
// [aboutWindow setDelegate:nil];
[NSApp terminate:self];
}
- (void)dealloc
{
[_lastOpenedImageView release];
[super dealloc];
}
@end