Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 175 lines (137 sloc) 5.922 kb
056cb11 initial commit
Philip Dow authored
1 //
2 // PDExportableImageView.m
3 // Journler
4 //
5 // Created by Philip Dow on 10/30/07.
6dc7e9e updated copyright notice in most project files
Philip Dow authored
6 // Copyright 2007 Sprouted, Philip Dow. All rights reserved.
056cb11 initial commit
Philip Dow authored
7 //
8
6616b42 @phildow Added BSD style license notification project wide
authored
9 /*
10 Redistribution and use in source and binary forms, with or without modification, are permitted
11 provided that the following conditions are met:
12
13 * Redistributions of source code must retain the above copyright notice, this list of conditions
14 and the following disclaimer.
15
16 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
17 and the following disclaimer in the documentation and/or other materials provided with the
18 distribution.
19
20 * Neither the name of the author nor the names of its contributors may be used to endorse or
21 promote products derived from this software without specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
24 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 // Basically, you can use the code in your free, commercial, private and public projects
34 // as long as you include the above notice and attribute the code to Philip Dow / Sprouted
35 // If you use this code in an app send me a note. I'd love to know how the code is used.
36
37 // Please also note that this copyright does not supersede any other copyrights applicable to
38 // open source code used herein. While explicit credit has been given in the Journler about box,
39 // it may be lacking in some instances in the source code. I will remedy this in future commits,
40 // and if you notice any please point them out.
41
056cb11 initial commit
Philip Dow authored
42 #import "PDExportableImageView.h"
43 #import <SproutedUtilities/SproutedUtilities.h>
44
45 @implementation PDExportableImageView
46
47 - (id)initWithFrame:(NSRect)frame {
48 self = [super initWithFrame:frame];
49 if (self) {
50 // Initialization code here.
51 [self setEditable:YES];
52 [self unregisterDraggedTypes];
53 }
54 return self;
55 }
56
57 - (id)initWithCoder:(NSCoder *)decoder
58 {
59 if ( self = [super initWithCoder:decoder] )
60 {
61 [self setEditable:YES];
62 [self unregisterDraggedTypes];
63 }
64 return self;
65 }
66
67 - (void) dealloc
68 {
69 [filename release];
70 [super dealloc];
71 }
72
73 - (NSString*) filename
74 {
75 return filename;
76 }
77
78 - (void) setFilename:(NSString*)aFilename
79 {
80 if ( filename != aFilename )
81 {
82 [filename release];
83 filename = [aFilename copyWithZone:[self zone]];
84 }
85 }
86
87 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
88 {
89 BOOL enabled = NO;
90 SEL action = [menuItem action];
91
92 if ( action == @selector(paste:) )
93 enabled = NO;
94 else if ( action == @selector(cut:) )
95 enabled = NO;
96 else if ( action == @selector(delete:) )
97 enabled = NO;
98 else if ( action == @selector(copy:) )
99 enabled = YES;
100 else if ( [super respondsToSelector:@selector(validateMenuItem:)] )
101 enabled = [super validateMenuItem:menuItem];
102
103 return enabled;
104 }
105
106 #pragma mark -
107
108 - (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal
109 {
110 // don't permit local drags - pretty specific functioning
111 return ( isLocal ? NSDragOperationNone : NSDragOperationCopy );
112 }
113
114 - (BOOL)ignoreModifierKeysWhileDragging
115 {
116 return YES;
117 }
118
119 - (void)mouseDown:(NSEvent *)theEvent
120 {
121 NSEvent *dragEvent;
122
123 [[self window] setAcceptsMouseMovedEvents:YES];
124 dragEvent = [[self window] nextEventMatchingMask:NSLeftMouseDraggedMask | NSLeftMouseUpMask];
125
126 if ([dragEvent type] == NSLeftMouseDragged)
127 {
128 NSSize imageSize;
129 NSPoint imageOrigin, localCoordinates;
130
131 NSPasteboard *pboard;
132 NSArray *types;
133 NSImage *myImage, *dragImage, *returnImage;
134
135 localCoordinates = [self convertPoint:[dragEvent locationInWindow] fromView:nil];
136
137 myImage = [self image];
138 dragImage = [myImage imageWithWidth:64 height:[myImage size].height * 64. / [myImage size].width];
139 returnImage = [[NSImage alloc] initWithSize:NSMakeSize([dragImage size].width+12, [dragImage size].height+12)];
140
141 [returnImage lockFocus];
142
143 [[NSColor colorWithCalibratedWhite:0.15 alpha:0.5] set];
144 [[NSBezierPath bezierPathWithRoundedRect:NSMakeRect(0,0,[returnImage size].width,[returnImage size].height) cornerRadius:7.0] fill];
145
146 [[NSColor colorWithCalibratedWhite:0.4 alpha:0.5] set];
147 [[NSBezierPath bezierPathWithRoundedRect:NSMakeRect(0,0,[returnImage size].width,[returnImage size].height) cornerRadius:7.0] stroke];
148
149 [dragImage compositeToPoint:NSMakePoint(6,6) operation:NSCompositeSourceOver fraction:0.9];
150 [returnImage unlockFocus];
151
152 imageSize = [returnImage size];
153 imageOrigin = NSMakePoint(localCoordinates.x-(imageSize.width*3/4.), localCoordinates.y-(imageSize.height/4.));
154
155 pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
156 if ( [self filename] != nil )
157 types = [NSArray arrayWithObjects:NSFilenamesPboardType, NSTIFFPboardType, nil];
158 else
159 types = [NSArray arrayWithObjects:NSTIFFPboardType, nil];
160
161 [pboard declareTypes:types owner:self];
162 [pboard setData:[[self image] TIFFRepresentation] forType:NSTIFFPboardType];
163
164 if ( [self filename] != nil )
165 [pboard setPropertyList:[NSArray arrayWithObject:[self filename]] forType:NSFilenamesPboardType];
166
167 [self dragImage:returnImage at:imageOrigin offset:NSZeroSize
168 event:theEvent pasteboard:[NSPasteboard pasteboardWithName:NSDragPboard] source:self slideBack:YES];
169 }
170
171 [[self window] setAcceptsMouseMovedEvents:NO];
172 }
173
174 @end
Something went wrong with that request. Please try again.