drop_target_win.h 4.4 KB

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