| 1 |
// |
| 2 |
// CRDropView.m |
| 3 |
// ImageSpliter |
| 4 |
// |
| 5 |
// Created by hiroshi tomioka on 13/02/12. |
| 6 |
// Copyright (c) 2013年 hiroshi tomioka. All rights reserved. |
| 7 |
// |
| 8 |
// 画像ドロップの為のメインウィンドウ |
| 9 |
|
| 10 |
#import "CRDropView.h" |
| 11 |
#import <Foundation/Foundation.h> |
| 12 |
#import "NSImage+data.h" |
| 13 |
|
| 14 |
@implementation CRDropView |
| 15 |
|
| 16 |
@synthesize _files; |
| 17 |
|
| 18 |
- (id)initWithFrame:(NSRect)frame |
| 19 |
{ |
| 20 |
self = [super initWithFrame:frame]; |
| 21 |
if (self) { |
| 22 |
// Initialization code here. |
| 23 |
self._files = [NSMutableArray arrayWithCapacity:1]; |
| 24 |
} |
| 25 |
|
| 26 |
return self; |
| 27 |
} |
| 28 |
|
| 29 |
- (void)awakeFromNib { |
| 30 |
self._files = [NSMutableArray arrayWithCapacity:1]; |
| 31 |
[self registerForDraggedTypes:[NSArray arrayWithObjects: |
| 32 |
NSColorPboardType, NSFilenamesPboardType, nil]]; |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
- (void)drawRect:(NSRect)dirtyRect |
| 37 |
{ |
| 38 |
// Drawing code here. |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender |
| 43 |
{ |
| 44 |
if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) |
| 45 |
== NSDragOperationGeneric) |
| 46 |
{ |
| 47 |
//this means that the sender is offering the type of operation we want |
| 48 |
//return that we want the NSDragOperationGeneric operation that they |
| 49 |
//are offering |
| 50 |
return NSDragOperationGeneric; |
| 51 |
} |
| 52 |
else |
| 53 |
{ |
| 54 |
//since they aren't offering the type of operation we want, we have |
| 55 |
//to tell them we aren't interested |
| 56 |
return NSDragOperationNone; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
- (void)draggingExited:(id <NSDraggingInfo>)sender |
| 61 |
{ |
| 62 |
//we aren't particularily interested in this so we will do nothing |
| 63 |
//this is one of the methods that we do not have to implement |
| 64 |
} |
| 65 |
|
| 66 |
- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender |
| 67 |
{ |
| 68 |
if ((NSDragOperationGeneric & [sender draggingSourceOperationMask]) |
| 69 |
== NSDragOperationGeneric) |
| 70 |
{ |
| 71 |
//this means that the sender is offering the type of operation we want |
| 72 |
//return that we want the NSDragOperationGeneric operation that they |
| 73 |
//are offering |
| 74 |
return NSDragOperationGeneric; |
| 75 |
} |
| 76 |
else |
| 77 |
{ |
| 78 |
//since they aren't offering the type of operation we want, we have |
| 79 |
//to tell them we aren't interested |
| 80 |
return NSDragOperationNone; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender |
| 86 |
{ |
| 87 |
NSPasteboard *paste = [sender draggingPasteboard]; |
| 88 |
//gets the dragging-specific pasteboard from the sender |
| 89 |
NSArray *types = [NSArray arrayWithObjects:NSTIFFPboardType, |
| 90 |
NSFilenamesPboardType, nil]; |
| 91 |
//a list of types that we can accept |
| 92 |
NSString *desiredType = [paste availableTypeFromArray:types]; |
| 93 |
NSData *carriedData = [paste dataForType:desiredType]; |
| 94 |
|
| 95 |
if (nil == carriedData) |
| 96 |
{ |
| 97 |
//the operation failed for some reason |
| 98 |
NSRunAlertPanel(@"Error", @"", |
| 99 |
nil, nil, nil); |
| 100 |
return NO; |
| 101 |
} |
| 102 |
else |
| 103 |
{ |
| 104 |
if ([desiredType isEqualToString:NSFilenamesPboardType]) |
| 105 |
{ |
| 106 |
NSArray *fileArray = |
| 107 |
[paste propertyListForType:@"NSFilenamesPboardType"]; |
| 108 |
//サブフォルダを含め、画像ファイルをNSArrayに追加 |
| 109 |
for (NSString *path in fileArray) { |
| 110 |
BOOL isDirectory; |
| 111 |
if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) { |
| 112 |
if (isDirectory) { |
| 113 |
NSArray* array = [[NSFileManager defaultManager] subpathsAtPath:path]; |
| 114 |
array = [array sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; |
| 115 |
|
| 116 |
for (NSString *filename in array) { |
| 117 |
filename = [path stringByAppendingPathComponent:filename]; |
| 118 |
if ([NSImage isImageFile:filename]) { |
| 119 |
NSLog(@"%@", filename); |
| 120 |
[self._files addObject:filename]; |
| 121 |
} |
| 122 |
} |
| 123 |
} else { |
| 124 |
if ([NSImage isImageFile:path]) { |
| 125 |
[self._files addObject:path]; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
//ファイルがドロップされた事への通知、これでTableViewが更新される |
| 133 |
NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:self._files, @"files", nil]; |
| 134 |
NSNotification* notification = [NSNotification notificationWithName:@"files droped" |
| 135 |
object:self |
| 136 |
userInfo:dic]; |
| 137 |
[[NSNotificationCenter defaultCenter] postNotification:notification]; |
| 138 |
|
| 139 |
} |
| 140 |
else |
| 141 |
{ |
| 142 |
//this can't happen |
| 143 |
NSAssert(NO, @"This can't happen"); |
| 144 |
return NO; |
| 145 |
} |
| 146 |
} |
| 147 |
return YES; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
@end |