summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/url_drop_target.mm
blob: d21cd67ec3d2a3e4d0070e7af513d6e63f75d17c (plain)
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
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/cocoa/url_drop_target.h"

#include "base/logging.h"
#import "third_party/mozilla/include/NSPasteboard+Utils.h"

@interface URLDropTargetHandler(Private)

// Gets the appropriate drag operation given the |NSDraggingInfo|.
- (NSDragOperation)getDragOperation:(id<NSDraggingInfo>)sender;

// Tell the window controller to hide the drop indicator.
- (void)hideIndicator;

@end  // @interface URLDropTargetHandler(Private)

@implementation URLDropTargetHandler

- (id)initWithView:(NSView*)view {
  if ((self = [super init])) {
    view_ = view;
    [view_ registerForDraggedTypes:
         [NSArray arrayWithObjects:kWebURLsWithTitlesPboardType,
                                   NSURLPboardType,
                                   NSStringPboardType,
                                   NSFilenamesPboardType,
                                   nil]];
  }
  return self;
}

// The following four methods implement parts of the |NSDraggingDestination|
// protocol, which the owner should "forward" to its |URLDropTargetHandler|
// (us).

- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
  return [self getDragOperation:sender];
}

- (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender {
  NSDragOperation dragOp = [self getDragOperation:sender];
  if (dragOp == NSDragOperationCopy) {
    // Just tell the window controller to update the indicator.
    NSPoint hoverPoint = [view_ convertPointFromBase:[sender draggingLocation]];
    [[view_ urlDropController] indicateDropURLsInView:view_ at:hoverPoint];
  }
  return dragOp;
}

- (void)draggingExited:(id<NSDraggingInfo>)sender {
  [self hideIndicator];
}

- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
  [self hideIndicator];

  NSPasteboard* pboard = [sender draggingPasteboard];
  if ([pboard containsURLData]) {
    NSArray* urls = nil;
    NSArray* titles;  // discarded
    [pboard getURLs:&urls andTitles:&titles];

    if ([urls count]) {
      // Tell the window controller about the dropped URL(s).
      NSPoint dropPoint =
          [view_ convertPointFromBase:[sender draggingLocation]];
      [[view_ urlDropController] dropURLs:urls inView:view_ at:dropPoint];
      return YES;
    }
  }

  return NO;
}

@end  // @implementation URLDropTargetHandler

@implementation URLDropTargetHandler(Private)

- (NSDragOperation)getDragOperation:(id<NSDraggingInfo>)sender {
  // Only allow the copy operation.
  return [sender draggingSourceOperationMask] & NSDragOperationCopy;
}

- (void)hideIndicator {
  [[view_ urlDropController] hideDropURLsIndicatorInView:view_];
}

@end  // @implementation URLDropTargetHandler(Private)