1 // Copyright (c) 2011 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 "ui/base/dragdrop/drop_target_win.h"
9 #include "base/logging.h"
13 IDropTargetHelper
* DropTargetWin::cached_drop_target_helper_
= NULL
;
15 DropTargetWin::DropTargetWin(HWND hwnd
)
19 HRESULT result
= RegisterDragDrop(hwnd
, this);
20 DCHECK(SUCCEEDED(result
));
23 DropTargetWin::~DropTargetWin() {
27 IDropTargetHelper
* DropTargetWin::DropHelper() {
28 if (!cached_drop_target_helper_
) {
29 CoCreateInstance(CLSID_DragDropHelper
, 0, CLSCTX_INPROC_SERVER
,
30 IID_IDropTargetHelper
,
31 reinterpret_cast<void**>(&cached_drop_target_helper_
));
33 return cached_drop_target_helper_
;
36 ///////////////////////////////////////////////////////////////////////////////
37 // DropTargetWin, IDropTarget implementation:
39 HRESULT
DropTargetWin::DragEnter(IDataObject
* data_object
,
41 POINTL cursor_position
,
43 // Tell the helper that we entered so it can update the drag image.
44 IDropTargetHelper
* drop_helper
= DropHelper();
46 drop_helper
->DragEnter(GetHWND(), data_object
,
47 reinterpret_cast<POINT
*>(&cursor_position
), *effect
);
50 current_data_object_
= data_object
;
51 POINT screen_pt
= { cursor_position
.x
, cursor_position
.y
};
52 *effect
= OnDragEnter(current_data_object_
, key_state
, screen_pt
, *effect
);
56 HRESULT
DropTargetWin::DragOver(DWORD key_state
,
57 POINTL cursor_position
,
59 // Tell the helper that we moved over it so it can update the drag image.
60 IDropTargetHelper
* drop_helper
= DropHelper();
62 drop_helper
->DragOver(reinterpret_cast<POINT
*>(&cursor_position
), *effect
);
64 POINT screen_pt
= { cursor_position
.x
, cursor_position
.y
};
65 *effect
= OnDragOver(current_data_object_
, key_state
, screen_pt
, *effect
);
69 HRESULT
DropTargetWin::DragLeave() {
70 // Tell the helper that we moved out of it so it can update the drag image.
71 IDropTargetHelper
* drop_helper
= DropHelper();
73 drop_helper
->DragLeave();
75 OnDragLeave(current_data_object_
);
77 current_data_object_
= NULL
;
81 HRESULT
DropTargetWin::Drop(IDataObject
* data_object
,
83 POINTL cursor_position
,
85 // Tell the helper that we dropped onto it so it can update the drag image.
86 IDropTargetHelper
* drop_helper
= DropHelper();
88 drop_helper
->Drop(current_data_object_
,
89 reinterpret_cast<POINT
*>(&cursor_position
), *effect
);
92 POINT screen_pt
= { cursor_position
.x
, cursor_position
.y
};
93 *effect
= OnDrop(current_data_object_
, key_state
, screen_pt
, *effect
);
97 ///////////////////////////////////////////////////////////////////////////////
98 // DropTargetWin, IUnknown implementation:
100 HRESULT
DropTargetWin::QueryInterface(const IID
& iid
, void** object
) {
102 if (IsEqualIID(iid
, IID_IUnknown
) || IsEqualIID(iid
, IID_IDropTarget
)) {
105 return E_NOINTERFACE
;
111 ULONG
DropTargetWin::AddRef() {
115 ULONG
DropTargetWin::Release() {
116 if (--ref_count_
== 0) {
123 DWORD
DropTargetWin::OnDragEnter(IDataObject
* data_object
,
125 POINT cursor_position
,
127 return DROPEFFECT_NONE
;
130 DWORD
DropTargetWin::OnDragOver(IDataObject
* data_object
,
132 POINT cursor_position
,
134 return DROPEFFECT_NONE
;
137 void DropTargetWin::OnDragLeave(IDataObject
* data_object
) {
140 DWORD
DropTargetWin::OnDrop(IDataObject
* data_object
,
142 POINT cursor_position
,
144 return DROPEFFECT_NONE
;