DropImageView.m 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // DropImageView.m
  3. // DragNDrop
  4. //
  5. // Created by Dominik Hofacker on 26.11.11.
  6. // Copyright (c) 2011 CCoding. All rights reserved.
  7. //
  8. #import "DropImageView.h"
  9. #import "ZGFileUtils.h"
  10. @implementation DropImageView
  11. @synthesize delegate = _delegate;
  12. - (id)initWithCoder:(NSCoder *)aDecoder {
  13. self = [super initWithCoder:aDecoder];
  14. if (self) {
  15. [self registeredDraggedTypes];
  16. }
  17. return self;
  18. }
  19. - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
  20. highlight = YES;
  21. [self setNeedsDisplay: YES];
  22. return NSDragOperationCopy;
  23. }
  24. - (void)draggingExited:(id<NSDraggingInfo>)sender {
  25. highlight = NO;
  26. [self setNeedsDisplay:YES];
  27. }
  28. - (void)drawRect:(NSRect)dirtyRect {
  29. [super drawRect:dirtyRect];
  30. if (highlight) {
  31. [[NSColor grayColor]set];
  32. [NSBezierPath setDefaultLineWidth:5];
  33. [NSBezierPath strokeRect:dirtyRect];
  34. }
  35. }
  36. - (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender {
  37. highlight = NO;
  38. [self setNeedsDisplay:YES];
  39. return YES;
  40. }
  41. - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
  42. if ([sender draggingSource] != self) {
  43. NSURL *fileURL = [NSURL URLFromPasteboard:[sender draggingPasteboard]];
  44. // NSString *filePath = [[fileURL path] stringByAppendingString:@"/"];
  45. NSString *filePath = [fileURL path];
  46. if ([ZGFileUtils pathIsDirectory:[fileURL path]]) {
  47. [_delegate dropImageView:self didSelectDirectory:filePath];
  48. } else {
  49. [_delegate dropImageView:self didSelectFile:filePath];
  50. }
  51. }
  52. return YES;
  53. }
  54. @end