blob: 9809255047835b58ab0baee134793fd17bd33020 [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.
initial.commitd7cae122008-07-26 21:49:384
initial.commitd7cae122008-07-26 21:49:385#include "base/base_drop_target.h"
6
[email protected]c7f4b6272008-09-30 20:50:517#include <shlobj.h>
8
initial.commitd7cae122008-07-26 21:49:389#include "base/logging.h"
10
11///////////////////////////////////////////////////////////////////////////////
12
13IDropTargetHelper* BaseDropTarget::cached_drop_target_helper_ = NULL;
14
15BaseDropTarget::BaseDropTarget(HWND hwnd)
[email protected]c7f4b6272008-09-30 20:50:5116 : hwnd_(hwnd),
17 suspend_(false),
18 ref_count_(0) {
initial.commitd7cae122008-07-26 21:49:3819 DCHECK(hwnd);
20 HRESULT result = RegisterDragDrop(hwnd, this);
[email protected]c7f4b6272008-09-30 20:50:5121 DCHECK(SUCCEEDED(result));
initial.commitd7cae122008-07-26 21:49:3822}
23
24BaseDropTarget::~BaseDropTarget() {
25}
26
27// static
28IDropTargetHelper* BaseDropTarget::DropHelper() {
29 if (!cached_drop_target_helper_) {
30 CoCreateInstance(CLSID_DragDropHelper, 0, CLSCTX_INPROC_SERVER,
31 IID_IDropTargetHelper,
32 reinterpret_cast<void**>(&cached_drop_target_helper_));
33 }
34 return cached_drop_target_helper_;
35}
36
37///////////////////////////////////////////////////////////////////////////////
38// BaseDropTarget, IDropTarget implementation:
39
40HRESULT BaseDropTarget::DragEnter(IDataObject* data_object,
41 DWORD key_state,
42 POINTL cursor_position,
43 DWORD* effect) {
44 // Tell the helper that we entered so it can update the drag image.
45 IDropTargetHelper* drop_helper = DropHelper();
46 if (drop_helper) {
47 drop_helper->DragEnter(GetHWND(), data_object,
48 reinterpret_cast<POINT*>(&cursor_position), *effect);
49 }
50
51 // You can't drag and drop within the same HWND.
52 if (suspend_) {
53 *effect = DROPEFFECT_NONE;
54 return S_OK;
55 }
56 current_data_object_ = data_object;
57 POINT screen_pt = { cursor_position.x, cursor_position.y };
58 *effect = OnDragEnter(current_data_object_, key_state, screen_pt, *effect);
59 return S_OK;
60}
61
62HRESULT BaseDropTarget::DragOver(DWORD key_state,
63 POINTL cursor_position,
64 DWORD* effect) {
65 // Tell the helper that we moved over it so it can update the drag image.
66 IDropTargetHelper* drop_helper = DropHelper();
67 if (drop_helper)
68 drop_helper->DragOver(reinterpret_cast<POINT*>(&cursor_position), *effect);
69
70 if (suspend_) {
71 *effect = DROPEFFECT_NONE;
72 return S_OK;
73 }
74
75 POINT screen_pt = { cursor_position.x, cursor_position.y };
76 *effect = OnDragOver(current_data_object_, key_state, screen_pt, *effect);
77 return S_OK;
78}
79
80HRESULT BaseDropTarget::DragLeave() {
81 // Tell the helper that we moved out of it so it can update the drag image.
82 IDropTargetHelper* drop_helper = DropHelper();
83 if (drop_helper)
84 drop_helper->DragLeave();
85
86 OnDragLeave(current_data_object_);
87
88 current_data_object_ = NULL;
89 return S_OK;
90}
91
92HRESULT BaseDropTarget::Drop(IDataObject* data_object,
93 DWORD key_state,
94 POINTL cursor_position,
95 DWORD* effect) {
96 // Tell the helper that we dropped onto it so it can update the drag image.
97 IDropTargetHelper* drop_helper = DropHelper();
98 if (drop_helper) {
99 drop_helper->Drop(current_data_object_,
100 reinterpret_cast<POINT*>(&cursor_position), *effect);
101 }
102
103 if (suspend_) {
104 *effect = DROPEFFECT_NONE;
105 return S_OK;
106 }
107
108 POINT screen_pt = { cursor_position.x, cursor_position.y };
109 *effect = OnDrop(current_data_object_, key_state, screen_pt, *effect);
110 return S_OK;
111}
112
113///////////////////////////////////////////////////////////////////////////////
114// BaseDropTarget, IUnknown implementation:
115
116HRESULT BaseDropTarget::QueryInterface(const IID& iid, void** object) {
117 *object = NULL;
118 if (IsEqualIID(iid, IID_IUnknown) || IsEqualIID(iid, IID_IDropTarget)) {
119 *object = this;
120 } else {
121 return E_NOINTERFACE;
122 }
123 AddRef();
124 return S_OK;
125}
126
127ULONG BaseDropTarget::AddRef() {
[email protected]c7f4b6272008-09-30 20:50:51128 return ++ref_count_;
initial.commitd7cae122008-07-26 21:49:38129}
130
131ULONG BaseDropTarget::Release() {
[email protected]c7f4b6272008-09-30 20:50:51132 if (--ref_count_ == 0) {
initial.commitd7cae122008-07-26 21:49:38133 delete this;
[email protected]c7f4b6272008-09-30 20:50:51134 return 0U;
initial.commitd7cae122008-07-26 21:49:38135 }
136 return ref_count_;
137}
138
139DWORD BaseDropTarget::OnDragEnter(IDataObject* data_object,
140 DWORD key_state,
141 POINT cursor_position,
142 DWORD effect) {
143 return DROPEFFECT_NONE;
144}
145
146DWORD BaseDropTarget::OnDragOver(IDataObject* data_object,
147 DWORD key_state,
148 POINT cursor_position,
149 DWORD effect) {
150 return DROPEFFECT_NONE;
151}
152
153void BaseDropTarget::OnDragLeave(IDataObject* data_object) {
154}
155
156DWORD BaseDropTarget::OnDrop(IDataObject* data_object,
157 DWORD key_state,
158 POINT cursor_position,
159 DWORD effect) {
160 return DROPEFFECT_NONE;
161}
license.botbf09a502008-08-24 00:55:55162