BsWin32DropTarget.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "Math/BsVector2I.h"
  6. #include <windows.h>
  7. #include <oleidl.h>
  8. namespace bs
  9. {
  10. /** @addtogroup Platform-Internal
  11. * @{
  12. */
  13. /**
  14. * Called by the OS when various drag and drop actions are performed over a window this control is registered for.
  15. *
  16. * @note
  17. * This class queues all messages receives by the OS (from the core thread), and then executes the queue on sim thread.
  18. * You should be wary of which methods are allowed to be called from which thread.
  19. */
  20. class Win32DropTarget : public IDropTarget
  21. {
  22. /** Type of drag and drop event. */
  23. enum class DropOpType
  24. {
  25. DragOver,
  26. Drop,
  27. Leave
  28. };
  29. /** Type of data that a drag and drop operation contains. */
  30. enum class DropOpDataType
  31. {
  32. FileList,
  33. None
  34. };
  35. /** Structure describing a drag and drop operation. */
  36. struct DropTargetOp
  37. {
  38. DropTargetOp(DropOpType _type, const Vector2I& _pos)
  39. :type(_type), position(_pos), dataType(DropOpDataType::None)
  40. { }
  41. DropOpType type;
  42. Vector2I position;
  43. DropOpDataType dataType;
  44. Vector<Path>* mFileList;
  45. };
  46. public:
  47. Win32DropTarget(HWND hWnd);
  48. ~Win32DropTarget();
  49. void registerWithOS();
  50. void unregisterWithOS();
  51. HRESULT __stdcall QueryInterface(REFIID iid, void** ppvObject) override;
  52. /** COM requirement. Increments objects reference count. */
  53. ULONG __stdcall AddRef() override;
  54. /** COM requirement. Decreases the objects reference count and deletes the object if its zero. */
  55. ULONG __stdcall Release() override;
  56. /**
  57. * Called by the OS when user enters the drop target area while dragging an object.
  58. *
  59. * @note Called on core thread.
  60. */
  61. HRESULT __stdcall DragEnter(IDataObject* pDataObj, DWORD grfKeyState, POINTL pt, DWORD* pdwEffect) override;
  62. /**
  63. * Called by the OS while user continues to drag an object over the drop target.
  64. *
  65. * @note Called on core thread.
  66. */
  67. HRESULT __stdcall DragOver(DWORD grfKeyState, POINTL pt, DWORD* pdwEffect) override;
  68. /**
  69. * Called by the OS when user leaves the drop target.
  70. *
  71. * @note Called on core thread.
  72. */
  73. HRESULT __stdcall DragLeave() override;
  74. /**
  75. * Called by the OS when the user ends the drag operation while over the drop target.
  76. *
  77. * @note Called on core thread.
  78. */
  79. HRESULT __stdcall Drop(IDataObject* pDataObj, DWORD grfKeyState, POINTL pt, DWORD* pdwEffect) override;
  80. /**
  81. * Registers a new drop target to monitor.
  82. *
  83. * @note Sim thread only.
  84. */
  85. void registerDropTarget(DropTarget* dropTarget);
  86. /**
  87. * Unregisters an existing drop target and stops monitoring it.
  88. *
  89. * @note Sim thread only.
  90. */
  91. void unregisterDropTarget(DropTarget* dropTarget);
  92. /**
  93. * Gets the total number of monitored drop targets.
  94. *
  95. * @note Sim thread only.
  96. */
  97. unsigned int getNumDropTargets() const;
  98. /** Called every frame by the sim thread. Internal method. */
  99. void update();
  100. private:
  101. /** Check if we support the data in the provided drag and drop data object. */
  102. bool isDataValid(IDataObject* data);
  103. /** Gets a file list from data. Caller must ensure that the data actually contains a file list. */
  104. Vector<Path>* getFileListFromData(IDataObject* data);
  105. private:
  106. Vector<DropTarget*> mDropTargets;
  107. LONG mRefCount;
  108. HWND mHWnd;
  109. bool mAcceptDrag;
  110. Vector<DropTargetOp> mQueuedDropOps;
  111. Vector<Vector<Path>*> mFileLists;
  112. Mutex mSync;
  113. };
  114. /** @} */
  115. }