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.
4
5#ifndef UI_BASE_DRAGDROP_DROP_TARGET_WIN_H_
6#define UI_BASE_DRAGDROP_DROP_TARGET_WIN_H_
7
8#include <objidl.h>
9
10#include "base/memory/ref_counted.h"
11#include "ui/base/ui_base_export.h"
12
13// Windows interface.
14struct IDropTargetHelper;
15
16namespace ui {
17
18// A DropTarget implementation that takes care of the nitty gritty
19// of dnd. While this class is concrete, subclasses will most likely
20// want to override various OnXXX methods.
21//
22// Because DropTarget is ref counted you shouldn't delete it directly,
23// rather wrap it in a scoped_refptr. Be sure and invoke RevokeDragDrop(m_hWnd)
24// before the HWND is deleted too.
25//
26// This class is meant to be used in a STA and is not multithread-safe.
27class UI_BASE_EXPORT DropTargetWin : public IDropTarget {
28 public:
29  // Create a new DropTarget associating it with the given HWND.
30  explicit DropTargetWin(HWND hwnd);
31  virtual ~DropTargetWin();
32
33  // IDropTarget implementation:
34  HRESULT __stdcall DragEnter(IDataObject* data_object,
35                              DWORD key_state,
36                              POINTL cursor_position,
37                              DWORD* effect);
38  HRESULT __stdcall DragOver(DWORD key_state,
39                             POINTL cursor_position,
40                             DWORD* effect);
41  HRESULT __stdcall DragLeave();
42  HRESULT __stdcall Drop(IDataObject* data_object,
43                         DWORD key_state,
44                         POINTL cursor_position,
45                         DWORD* effect);
46
47  // IUnknown implementation:
48  HRESULT __stdcall QueryInterface(const IID& iid, void** object);
49  ULONG __stdcall AddRef();
50  ULONG __stdcall Release();
51
52 protected:
53  // Returns the hosting HWND.
54  HWND GetHWND() { return hwnd_; }
55
56  // Invoked when the cursor first moves over the hwnd during a dnd session.
57  // This should return a bitmask of the supported drop operations:
58  // DROPEFFECT_NONE, DROPEFFECT_COPY, DROPEFFECT_LINK and/or
59  // DROPEFFECT_MOVE.
60  virtual DWORD OnDragEnter(IDataObject* data_object,
61                            DWORD key_state,
62                            POINT cursor_position,
63                            DWORD effect);
64
65  // Invoked when the cursor moves over the window during a dnd session.
66  // This should return a bitmask of the supported drop operations:
67  // DROPEFFECT_NONE, DROPEFFECT_COPY, DROPEFFECT_LINK and/or
68  // DROPEFFECT_MOVE.
69  virtual DWORD OnDragOver(IDataObject* data_object,
70                           DWORD key_state,
71                           POINT cursor_position,
72                           DWORD effect);
73
74  // Invoked when the cursor moves outside the bounds of the hwnd during a
75  // dnd session.
76  virtual void OnDragLeave(IDataObject* data_object);
77
78  // Invoked when the drop ends on the window. This should return the operation
79  // that was taken.
80  virtual DWORD OnDrop(IDataObject* data_object,
81                       DWORD key_state,
82                       POINT cursor_position,
83                       DWORD effect);
84
85 private:
86  // Returns the cached drop helper, creating one if necessary. The returned
87  // object is not addrefed. May return NULL if the object couldn't be created.
88  static IDropTargetHelper* DropHelper();
89
90  // The data object currently being dragged over this drop target.
91  scoped_refptr<IDataObject> current_data_object_;
92
93  // A helper object that is used to provide drag image support while the mouse
94  // is dragging over the content area.
95  //
96  // DO NOT ACCESS DIRECTLY! Use DropHelper() instead, which will lazily create
97  // this if it doesn't exist yet. This object can take tens of milliseconds to
98  // create, and we don't want to block any window opening for this, especially
99  // since often, DnD will never be used. Instead, we force this penalty to the
100  // first time it is actually used.
101  static IDropTargetHelper* cached_drop_target_helper_;
102
103  // The HWND of the source. This HWND is used to determine coordinates for
104  // mouse events that are sent to the renderer notifying various drag states.
105  HWND hwnd_;
106
107  LONG ref_count_;
108
109  DISALLOW_COPY_AND_ASSIGN(DropTargetWin);
110};
111
112}  // namespace ui
113
114#endif  // UI_BASE_DRAGDROP_DROP_TARGET_WIN_H_
115