Port PluginObject fix downstream. See http://trac.webkit.org/changeset/61415/ for...
[chromium-blink-merge.git] / base / base_drop_target.cc
blob1ee878feac6c2335d11a0c663aca2d9acd68e04d
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/base_drop_target.h"
7 #include <shlobj.h>
9 #include "base/logging.h"
11 ///////////////////////////////////////////////////////////////////////////////
13 IDropTargetHelper* BaseDropTarget::cached_drop_target_helper_ = NULL;
14 int32 BaseDropTarget::drag_identity_ = 0;
16 BaseDropTarget::BaseDropTarget(HWND hwnd)
17 : hwnd_(hwnd),
18 suspended_(false),
19 ref_count_(0) {
20 DCHECK(hwnd);
21 HRESULT result = RegisterDragDrop(hwnd, this);
22 DCHECK(SUCCEEDED(result));
25 BaseDropTarget::~BaseDropTarget() {
28 // static
29 IDropTargetHelper* BaseDropTarget::DropHelper() {
30 if (!cached_drop_target_helper_) {
31 CoCreateInstance(CLSID_DragDropHelper, 0, CLSCTX_INPROC_SERVER,
32 IID_IDropTargetHelper,
33 reinterpret_cast<void**>(&cached_drop_target_helper_));
35 return cached_drop_target_helper_;
38 ///////////////////////////////////////////////////////////////////////////////
39 // BaseDropTarget, IDropTarget implementation:
41 HRESULT BaseDropTarget::DragEnter(IDataObject* data_object,
42 DWORD key_state,
43 POINTL cursor_position,
44 DWORD* effect) {
45 // Tell the helper that we entered so it can update the drag image.
46 IDropTargetHelper* drop_helper = DropHelper();
47 if (drop_helper) {
48 drop_helper->DragEnter(GetHWND(), data_object,
49 reinterpret_cast<POINT*>(&cursor_position), *effect);
52 // You can't drag and drop within the same HWND.
53 if (suspended_) {
54 *effect = DROPEFFECT_NONE;
55 return S_OK;
58 // Update the drag identity, skipping 0.
59 if (++drag_identity_ == 0)
60 ++drag_identity_;
62 current_data_object_ = data_object;
63 POINT screen_pt = { cursor_position.x, cursor_position.y };
64 *effect = OnDragEnter(current_data_object_, key_state, screen_pt, *effect);
65 return S_OK;
68 HRESULT BaseDropTarget::DragOver(DWORD key_state,
69 POINTL cursor_position,
70 DWORD* effect) {
71 // Tell the helper that we moved over it so it can update the drag image.
72 IDropTargetHelper* drop_helper = DropHelper();
73 if (drop_helper)
74 drop_helper->DragOver(reinterpret_cast<POINT*>(&cursor_position), *effect);
76 if (suspended_) {
77 *effect = DROPEFFECT_NONE;
78 return S_OK;
81 POINT screen_pt = { cursor_position.x, cursor_position.y };
82 *effect = OnDragOver(current_data_object_, key_state, screen_pt, *effect);
83 return S_OK;
86 HRESULT BaseDropTarget::DragLeave() {
87 // Tell the helper that we moved out of it so it can update the drag image.
88 IDropTargetHelper* drop_helper = DropHelper();
89 if (drop_helper)
90 drop_helper->DragLeave();
92 if (suspended_)
93 return S_OK;
95 OnDragLeave(current_data_object_);
97 current_data_object_ = NULL;
98 return S_OK;
101 HRESULT BaseDropTarget::Drop(IDataObject* data_object,
102 DWORD key_state,
103 POINTL cursor_position,
104 DWORD* effect) {
105 // Tell the helper that we dropped onto it so it can update the drag image.
106 IDropTargetHelper* drop_helper = DropHelper();
107 if (drop_helper) {
108 drop_helper->Drop(current_data_object_,
109 reinterpret_cast<POINT*>(&cursor_position), *effect);
112 if (suspended_) {
113 *effect = DROPEFFECT_NONE;
114 return S_OK;
117 POINT screen_pt = { cursor_position.x, cursor_position.y };
118 *effect = OnDrop(current_data_object_, key_state, screen_pt, *effect);
119 return S_OK;
122 ///////////////////////////////////////////////////////////////////////////////
123 // BaseDropTarget, IUnknown implementation:
125 HRESULT BaseDropTarget::QueryInterface(const IID& iid, void** object) {
126 *object = NULL;
127 if (IsEqualIID(iid, IID_IUnknown) || IsEqualIID(iid, IID_IDropTarget)) {
128 *object = this;
129 } else {
130 return E_NOINTERFACE;
132 AddRef();
133 return S_OK;
136 ULONG BaseDropTarget::AddRef() {
137 return ++ref_count_;
140 ULONG BaseDropTarget::Release() {
141 if (--ref_count_ == 0) {
142 delete this;
143 return 0U;
145 return ref_count_;
148 DWORD BaseDropTarget::OnDragEnter(IDataObject* data_object,
149 DWORD key_state,
150 POINT cursor_position,
151 DWORD effect) {
152 return DROPEFFECT_NONE;
155 DWORD BaseDropTarget::OnDragOver(IDataObject* data_object,
156 DWORD key_state,
157 POINT cursor_position,
158 DWORD effect) {
159 return DROPEFFECT_NONE;
162 void BaseDropTarget::OnDragLeave(IDataObject* data_object) {
165 DWORD BaseDropTarget::OnDrop(IDataObject* data_object,
166 DWORD key_state,
167 POINT cursor_position,
168 DWORD effect) {
169 return DROPEFFECT_NONE;