summaryrefslogtreecommitdiffstats
path: root/views/widget/drop_helper.cc
blob: f15134b007de00e4be780b627eac0e1fc1bb1239 (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
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
// Copyright (c) 2006-2008 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.

#include "views/widget/drop_helper.h"

#include "ui/base/dragdrop/drag_drop_types.h"
#include "views/view.h"
#include "views/widget/root_view.h"

namespace views {

DropHelper::DropHelper(RootView* root_view)
    : root_view_(root_view),
      target_view_(NULL),
      deepest_view_(NULL) {
}

DropHelper::~DropHelper() {
}

void DropHelper::ResetTargetViewIfEquals(View* view) {
  if (target_view_ == view)
    target_view_ = NULL;
  if (deepest_view_ == view)
    deepest_view_ = NULL;
}

int DropHelper::OnDragOver(const OSExchangeData& data,
                           const gfx::Point& root_view_location,
                           int drag_operation) {
  View* view = CalculateTargetViewImpl(root_view_location, data, true,
                                       &deepest_view_);

  if (view != target_view_) {
    // Target changed notify old drag exited, then new drag entered.
    NotifyDragExit();
    target_view_ = view;
    NotifyDragEntered(data, root_view_location, drag_operation);
  }

  return NotifyDragOver(data, root_view_location, drag_operation);
}

void DropHelper::OnDragExit() {
  NotifyDragExit();
  deepest_view_ = target_view_ = NULL;
}

int DropHelper::OnDrop(const OSExchangeData& data,
                       const gfx::Point& root_view_location,
                       int drag_operation) {
  View* drop_view = target_view_;
  deepest_view_ = target_view_ = NULL;
  if (!drop_view)
    return ui::DragDropTypes::DRAG_NONE;

  if (drag_operation == ui::DragDropTypes::DRAG_NONE) {
    drop_view->OnDragExited();
    return ui::DragDropTypes::DRAG_NONE;
  }

  gfx::Point view_location(root_view_location);
  View::ConvertPointToView(NULL, drop_view, &view_location);
  DropTargetEvent drop_event(data, view_location.x(), view_location.y(),
                             drag_operation);
  return drop_view->OnPerformDrop(drop_event);
}

View* DropHelper::CalculateTargetView(
    const gfx::Point& root_view_location,
    const OSExchangeData& data,
    bool check_can_drop) {
  return CalculateTargetViewImpl(root_view_location, data, check_can_drop,
                                 NULL);
}

View* DropHelper::CalculateTargetViewImpl(
    const gfx::Point& root_view_location,
    const OSExchangeData& data,
    bool check_can_drop,
    View** deepest_view) {
  View* view = root_view_->GetViewForPoint(root_view_location);
  if (view == deepest_view_) {
    // The view the mouse is over hasn't changed; reuse the target.
    return target_view_;
  }
  if (deepest_view)
    *deepest_view = view;
  // TODO(sky): for the time being these are separate. Once I port chrome menu
  // I can switch to the #else implementation and nuke the OS_WIN
  // implementation.
#if defined(OS_WIN)
  // View under mouse changed, which means a new view may want the drop.
  // Walk the tree, stopping at target_view_ as we know it'll accept the
  // drop.
  while (view && view != target_view_ &&
         (!view->IsEnabled() || !view->CanDrop(data))) {
    view = view->GetParent();
  }
#else
  int formats = 0;
  std::set<OSExchangeData::CustomFormat> custom_formats;
  while (view && view != target_view_) {
    if (view->IsEnabled() &&
        view->GetDropFormats(&formats, &custom_formats) &&
        data.HasAnyFormat(formats, custom_formats) &&
        (!check_can_drop || view->CanDrop(data))) {
      // Found the view.
      return view;
    }
    formats = 0;
    custom_formats.clear();
    view = view->GetParent();
  }
#endif
  return view;
}

void DropHelper::NotifyDragEntered(const OSExchangeData& data,
                                   const gfx::Point& root_view_location,
                                   int drag_operation) {
  if (!target_view_)
    return;

  gfx::Point target_view_location(root_view_location);
  View::ConvertPointToView(root_view_, target_view_, &target_view_location);
  DropTargetEvent enter_event(data,
                              target_view_location.x(),
                              target_view_location.y(),
                              drag_operation);
  target_view_->OnDragEntered(enter_event);
}

int DropHelper::NotifyDragOver(const OSExchangeData& data,
                               const gfx::Point& root_view_location,
                               int drag_operation) {
  if (!target_view_)
    return ui::DragDropTypes::DRAG_NONE;

  gfx::Point target_view_location(root_view_location);
  View::ConvertPointToView(root_view_, target_view_, &target_view_location);
  DropTargetEvent enter_event(data,
                              target_view_location.x(),
                              target_view_location.y(),
                              drag_operation);
  return target_view_->OnDragUpdated(enter_event);
}

void DropHelper::NotifyDragExit() {
  if (target_view_)
    target_view_->OnDragExited();
}

}  // namespace views