Also ran 'git cl format' on this file since I was touching a lot of it anyway.
[chromium-blink-merge.git] / ui / base / dragdrop / drop_target_win.cc
blob0370e2bc52d1ee10f58d3c3447dce4b31012a5dc
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"
7 #include <shlobj.h>
9 #include "base/logging.h"
11 namespace ui {
13 IDropTargetHelper* DropTargetWin::cached_drop_target_helper_ = NULL;
15 DropTargetWin::DropTargetWin(HWND hwnd)
16 : hwnd_(hwnd),
17 ref_count_(0) {
18 DCHECK(hwnd);
19 HRESULT result = RegisterDragDrop(hwnd, this);
20 DCHECK(SUCCEEDED(result));
23 DropTargetWin::~DropTargetWin() {
26 // static
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,
40 DWORD key_state,
41 POINTL cursor_position,
42 DWORD* effect) {
43 // Tell the helper that we entered so it can update the drag image.
44 IDropTargetHelper* drop_helper = DropHelper();
45 if (drop_helper) {
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 =
53 OnDragEnter(current_data_object_.get(), key_state, screen_pt, *effect);
54 return S_OK;
57 HRESULT DropTargetWin::DragOver(DWORD key_state,
58 POINTL cursor_position,
59 DWORD* effect) {
60 // Tell the helper that we moved over it so it can update the drag image.
61 IDropTargetHelper* drop_helper = DropHelper();
62 if (drop_helper)
63 drop_helper->DragOver(reinterpret_cast<POINT*>(&cursor_position), *effect);
65 POINT screen_pt = { cursor_position.x, cursor_position.y };
66 *effect =
67 OnDragOver(current_data_object_.get(), key_state, screen_pt, *effect);
68 return S_OK;
71 HRESULT DropTargetWin::DragLeave() {
72 // Tell the helper that we moved out of it so it can update the drag image.
73 IDropTargetHelper* drop_helper = DropHelper();
74 if (drop_helper)
75 drop_helper->DragLeave();
77 OnDragLeave(current_data_object_.get());
79 current_data_object_ = NULL;
80 return S_OK;
83 HRESULT DropTargetWin::Drop(IDataObject* data_object,
84 DWORD key_state,
85 POINTL cursor_position,
86 DWORD* effect) {
87 // Tell the helper that we dropped onto it so it can update the drag image.
88 IDropTargetHelper* drop_helper = DropHelper();
89 if (drop_helper) {
90 drop_helper->Drop(current_data_object_.get(),
91 reinterpret_cast<POINT*>(&cursor_position), *effect);
94 POINT screen_pt = { cursor_position.x, cursor_position.y };
95 *effect = OnDrop(current_data_object_.get(), key_state, screen_pt, *effect);
96 return S_OK;
99 ///////////////////////////////////////////////////////////////////////////////
100 // DropTargetWin, IUnknown implementation:
102 HRESULT DropTargetWin::QueryInterface(const IID& iid, void** object) {
103 *object = NULL;
104 if (IsEqualIID(iid, IID_IUnknown) || IsEqualIID(iid, IID_IDropTarget)) {
105 *object = this;
106 } else {
107 return E_NOINTERFACE;
109 AddRef();
110 return S_OK;
113 ULONG DropTargetWin::AddRef() {
114 return ++ref_count_;
117 ULONG DropTargetWin::Release() {
118 if (--ref_count_ == 0) {
119 delete this;
120 return 0U;
122 return ref_count_;
125 DWORD DropTargetWin::OnDragEnter(IDataObject* data_object,
126 DWORD key_state,
127 POINT cursor_position,
128 DWORD effect) {
129 return DROPEFFECT_NONE;
132 DWORD DropTargetWin::OnDragOver(IDataObject* data_object,
133 DWORD key_state,
134 POINT cursor_position,
135 DWORD effect) {
136 return DROPEFFECT_NONE;
139 void DropTargetWin::OnDragLeave(IDataObject* data_object) {
142 DWORD DropTargetWin::OnDrop(IDataObject* data_object,
143 DWORD key_state,
144 POINT cursor_position,
145 DWORD effect) {
146 return DROPEFFECT_NONE;
149 } // namespace ui