-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSimpleViewerImageView.m
More file actions
241 lines (207 loc) · 6.56 KB
/
SimpleViewerImageView.m
File metadata and controls
241 lines (207 loc) · 6.56 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// SimpleViewerView.m
// SimpleCap
//
// Created by - on 08/12/21.
// Copyright 2008 Hiroshi Hashiguchi. All rights reserved.
//
#import "SimpleViewerImageView.h"
#import "SimpleViewerImageSubView.h"
#import "SimpleViewerController.h"
#import "UserDefaults.h"
#define TRANSITION_DURATION 0.25
@implementation SimpleViewerImageView
- (id)initWithFrame:(NSRect)frame withController:(SimpleViewerController*)controller {
self = [super initWithFrame:frame];
if (self) {
_controller = [controller retain];
_current_index = 0;
_is_transition = NO;
_image_views = [[NSMutableArray alloc] init];
[_image_views addObject:
[[[SimpleViewerImageSubView alloc] initWithFrame:frame] autorelease]];
[_image_views addObject:
[[[SimpleViewerImageSubView alloc] initWithFrame:frame] autorelease]];
_transition = [[CATransition animation] retain];
[_transition setDelegate:self];
[self setAnimations:
[NSDictionary dictionaryWithObject:_transition forKey:@"subviews"]];
[[self animator] addSubview:[_image_views objectAtIndex:_current_index]];
[self setWantsLayer:YES];
}
return self;
}
- (void) dealloc
{
[_controller release];
[_transition release];
// [_subviews release];
[super dealloc];
}
- (void)drawRect:(NSRect)rect {
}
- (void)setImage:(NSImage*)image withDirection:(int)direction
{
NSString* tran_type;
NSString* tran_subtype;
NSTimeInterval duration = TRANSITION_DURATION;
switch (direction) {
case SV_TRANTYPE_NEXT:
tran_type = kCATransitionPush;
tran_subtype = kCATransitionFromRight;
break;
case SV_TRANTYPE_TRASH:
tran_type = kCATransitionReveal;
tran_subtype = kCATransitionFromLeft;
duration = TRANSITION_DURATION*2;
break;
case SV_TRANTYPE_PREVIOUS:
tran_type = kCATransitionPush;
tran_subtype = kCATransitionFromLeft;
break;
case SV_TRANTYPE_NEW:
tran_type = kCATransitionMoveIn;
tran_subtype = kCATransitionFromTop;
duration = TRANSITION_DURATION*2;
break;
case SV_TRANTYPE_OPEN:
tran_type = kCATransitionFade;
tran_subtype = kCATransitionFromRight;
duration = TRANSITION_DURATION*2;
break;
case SV_TRANTYPE_UPDATE:
tran_type = kCATransitionFade;
tran_subtype = kCATransitionFromTop;
duration = TRANSITION_DURATION*2;
break;
case SV_TRANTYPE_NONE:
default:
tran_type = kCATransitionPush;
tran_subtype = kCATransitionFromTop;
break;
}
[_transition setDuration:duration];
[_transition setType:tran_type];
[_transition setSubtype:tran_subtype];
if (_is_transition) {
int next_index = (_current_index + 1) % 2;
[[_image_views objectAtIndex:next_index] setImage:image];
[[self animator] replaceSubview:[_image_views objectAtIndex:_current_index]
with:[_image_views objectAtIndex:next_index]];
_current_index = next_index;
} else {
[[_image_views objectAtIndex:_current_index] setImage:image];
_is_transition = YES;
}
}
- (NSImage*)image
{
return [[_image_views objectAtIndex:_current_index] image];
}
- (CGFloat)reductionRatio
{
return [[_image_views objectAtIndex:_current_index] reductionRatio];
}
- (void)setFrame:(NSRect)frame
{
[super setFrame:frame];
frame.origin = NSZeroPoint;
for (NSView* view in [self subviews]) {
[view setFrame:frame];
}
}
- (void)setNeedsDisplay:(BOOL)flag
{
[super setNeedsDisplay:flag];
for (NSView* view in [self subviews]) {
[view setNeedsDisplay:flag];
}
}
- (void)animationDidStart:(CAAnimation *)theAnimation
{
}
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
}
- (NSMenu *)menuForEvent:(NSEvent *)theEvent
{
return [_controller menuForEvent:theEvent];
}
- (NSArray *)namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination
{
[_controller copyFileTo:dropDestination];
return [NSArray arrayWithObject:[[_controller filename] lastPathComponent]];
}
- (void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSString *)type
{
// NSLog(@"%@", type);
if ([type isEqualToString:NSTIFFPboardType]) {
[sender setData:[[self image] TIFFRepresentation] forType:NSTIFFPboardType];
}
}
- (void)mouseDown:(NSEvent *)theEvent
{
NSImage* image = [self image];
NSString* filename = [_controller filename];
CGFloat ratio = [self reductionRatio];
NSRect bounds = [self bounds];
NSRect image_rect = NSZeroRect;
image_rect.size.width = image.size.width * ratio;
image_rect.size.height = image.size.height * ratio;
image_rect.origin.x = (int)((bounds.size.width - image_rect.size.width)/2.0);
image_rect.origin.y = (int)((bounds.size.height - image_rect.size.height)/2.0);
NSPoint p = [self convertPoint:[theEvent locationInWindow] fromView:nil];
if (NSPointInRect(p, image_rect)) {
NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[pboard declareTypes:[NSArray arrayWithObjects:NSTIFFPboardType, NSFilesPromisePboardType, nil] owner:self];
[pboard setPropertyList:[NSArray arrayWithObject:[filename pathExtension]]
forType:NSFilesPromisePboardType];
/*
[pboard setPropertyList:[NSArray arrayWithObject:filename]
forType:NSFilenamesPboardType];
[pboard setString:[[NSURL fileURLWithPath:filename] absoluteString] forType:NSStringPboardType];
// can not work below for Safari
[[NSURL fileURLWithPath:filename] writeToPasteboard:pboard];
*/
NSImage *dragged_image = [[[NSImage alloc] initWithSize:image_rect.size] autorelease];
[dragged_image lockFocus];
[image drawInRect:NSMakeRect(0, 0, image_rect.size.width, image_rect.size.height)
fromRect:NSMakeRect(0, 0, image.size.width, image.size.height)
operation:NSCompositeSourceOver
fraction:0.5f];
[dragged_image unlockFocus];
[self dragImage:dragged_image
at:NSMakePoint((bounds.size.width - image_rect.size.width)/2.0, (bounds.size.height - image_rect.size.height)/2.0)
offset:NSZeroSize
event:theEvent
pasteboard:pboard
source:self
slideBack:YES];
} else {
// same code exists other file
NSWindow *window = [self window];
NSPoint origin = [window frame].origin;
NSPoint old_p = [window convertBaseToScreen:[theEvent locationInWindow]];
while ((theEvent = [window nextEventMatchingMask:NSLeftMouseDraggedMask|NSLeftMouseUpMask]) && ([theEvent type] != NSLeftMouseUp)) {
NSPoint new_p = [window convertBaseToScreen:[theEvent locationInWindow]];
origin.x += new_p.x - old_p.x;
origin.y += new_p.y - old_p.y;
[window setFrameOrigin:origin];
old_p = new_p;
}
}
}
- (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal
{
return NSDragOperationCopy;
}
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
return YES;
}
/* can not be called (why?) 2009-02-22 */
- (BOOL)mouseDownCanMoveWindow
{
return NO;
}
@end