/
GLImageView-Delegate.m
195 lines (172 loc) · 4.49 KB
/
GLImageView-Delegate.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
//
// GLImageView.h
// GameLauncher
//
// Created by Adam on 22/06/05.
// Copyright 2005 2Sublime.com. All rights reserved.
//
#import "GLImageView-Delegate.h"
@implementation GLImageView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
[self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
return self;
}
- (void)dealloc
{
[self unregisterDraggedTypes];
[super dealloc];
}
- (void)awakeFromNib
{
[self setEnabled:YES];
[self setImageScaling:NSScaleToFit];
[self setImageFrameStyle:NSImageFrameGrayBezel];
}
- (void)drawRect:(NSRect)rect
{
[super drawRect:rect];
if (highlighted) {
NSColor *hlColor = [[NSColor selectedTextBackgroundColor] shadowWithLevel:0.2];
[hlColor set];
[NSBezierPath setDefaultLineWidth:2.5];
[NSBezierPath strokeRect:NSInsetRect(rect, 7.0, 7.0)];
}
}
- (void)viewDidEndLiveResize
{
[self setNeedsDisplay:YES];
}
#pragma mark Dragging destination
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
== NSDragOperationGeneric)
{
highlighted = YES;
[self setNeedsDisplay:YES];
return NSDragOperationGeneric;
}
else
{
return NSDragOperationNone;
}
}
- (void)draggingExited:(id <NSDraggingInfo>)sender
{
highlighted = NO;
[self setNeedsDisplay:YES];
}
/*- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
{
if ((NSDragOperationGeneric & [sender draggingSourceOperationMask])
== NSDragOperationGeneric)
{
return NSDragOperationGeneric;
}
else
{
return NSDragOperationNone;
}
}
- (void)draggingEnded:(id <NSDraggingInfo>)sender
{
}*/
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
return YES;
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
NSPasteboard *paste = [sender draggingPasteboard];
//gets the dragging-specific pasteboard from the sender
NSArray *types = [NSArray arrayWithObjects:NSFilenamesPboardType, nil];
//a list of types that we can accept
NSString *desiredType = [paste availableTypeFromArray:types];
NSData *carriedData = [paste dataForType:desiredType];
if (nil == carriedData)
{
//the operation failed for some reason
NSRunAlertPanel(@"Paste Error", @"Sorry, but the operation failed",
nil, nil, nil);
return NO;
}
else
{
if ([desiredType isEqualToString:NSFilenamesPboardType])
{
//we have a list of file names in an NSData object
//be caseful since this method returns id.
//We just happen to know that it will be an array.
NSArray *fileArray =
[paste propertyListForType:@"NSFilenamesPboardType"];
NSString *path = [fileArray objectAtIndex:0];
[[GLGUIController sharedInstance] setLaunchAppPath:path];
}
}
[self setNeedsDisplay:YES]; //redraw us with the new image
return YES;
}
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
highlighted = NO;
[self setNeedsDisplay:YES];
}
/*
#pragma mark Dragging Destination
- (unsigned int)draggingEntered:(id <NSDraggingInfo>)sender
{
NSLog(@"Dragging Entered");
if ([sender draggingSource] != self) {
NSPasteboard *pb = [sender draggingPasteboard];
NSString *type = [pb availableTypeFromArray:[NSArray
arrayWithObject:NSFilenamesPboardType]];
if (type != nil) {
// [imageWell highlight:YES
// withFrame:[imageWell bounds]
// inView:nil]
// [imageWell setNeedsDisplay:YES];
}
}
}
- (void)draggingExited:(id <NSDraggingInfo>)sender
{
NSLog(@"Dragging Exited");
[imageWell setNeedsDisplay:YES];
}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
return YES;
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
NSPasteboard *pb = [sender draggingPasteboard];
if (![self readStringFromPasteboard:pb]) {
NSLog(@"Error: Couldn't read from dragging pasteboard");
return NO;
}
return YES;
}
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
NSLog(@"Conclude dragging operation");
[imageWell setNeedsDisplay:YES];
}
- (BOOL)readStringFromPasteboard:(NSPasteboard *)pb
{
NSString *value;
NSString *type;
//is there a string in pb?
type = [pb availableTypeFromArray:[NSArray
arrayWithObject:NSFilenamesPboardType]];
if (type) {
//read the string from pb
value = [pb stringForType:NSFilenamesPboardType];
NSLog(@"%@", value);
return YES;
}
return NO;
}
*/
@end