license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
| 5 | #ifndef BASE_BASE_DROP_TARGET_H__ |
| 6 | #define BASE_BASE_DROP_TARGET_H__ |
| 7 | |
| 8 | #include <atlbase.h> |
| 9 | #include <objidl.h> |
| 10 | #include <shobjidl.h> |
| 11 | |
| 12 | #include "base/basictypes.h" |
| 13 | |
| 14 | // A DropTarget implementation that takes care of the nitty gritty |
| 15 | // of dnd. While this class is concrete, subclasses will most likely |
| 16 | // want to override various OnXXX methods. |
| 17 | // |
| 18 | // Because BaseDropTarget is ref counted you shouldn't delete it directly, |
| 19 | // rather wrap it in a scoped_refptr. Be sure and invoke RevokeDragDrop(m_hWnd) |
| 20 | // before the HWND is deleted too. |
| 21 | class BaseDropTarget : public IDropTarget { |
| 22 | public: |
| 23 | // Create a new BaseDropTarget associating it with the given HWND. |
| 24 | explicit BaseDropTarget(HWND hwnd); |
| 25 | virtual ~BaseDropTarget(); |
| 26 | |
| 27 | // When suspend is set to |true|, the drop target does not receive drops from |
| 28 | // drags initiated within the owning HWND. |
| 29 | // TODO(beng): (http://b/1085385) figure out how we will handle legitimate |
| 30 | // drag-drop operations within the same HWND, such as dragging |
| 31 | // selected text to an edit field. |
| 32 | void set_suspend(bool suspend) { suspend_ = suspend; } |
| 33 | |
| 34 | // IDropTarget implementation: |
| 35 | HRESULT __stdcall DragEnter(IDataObject* data_object, |
| 36 | DWORD key_state, |
| 37 | POINTL cursor_position, |
| 38 | DWORD* effect); |
| 39 | HRESULT __stdcall DragOver(DWORD key_state, |
| 40 | POINTL cursor_position, |
| 41 | DWORD* effect); |
| 42 | HRESULT __stdcall DragLeave(); |
| 43 | HRESULT __stdcall Drop(IDataObject* data_object, |
| 44 | DWORD key_state, |
| 45 | POINTL cursor_position, |
| 46 | DWORD* effect); |
| 47 | |
| 48 | // IUnknown implementation: |
| 49 | HRESULT __stdcall QueryInterface(const IID& iid, void** object); |
| 50 | ULONG __stdcall AddRef(); |
| 51 | ULONG __stdcall Release(); |
| 52 | |
| 53 | protected: |
| 54 | // Returns the hosting HWND. |
| 55 | HWND GetHWND() { return hwnd_; } |
| 56 | |
| 57 | // Invoked when the cursor first moves over the hwnd 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 OnDragEnter(IDataObject* data_object, |
| 62 | DWORD key_state, |
| 63 | POINT cursor_position, |
| 64 | DWORD effect); |
| 65 | |
| 66 | // Invoked when the cursor moves over the window during a dnd session. |
| 67 | // This should return a bitmask of the supported drop operations: |
| 68 | // DROPEFFECT_NONE, DROPEFFECT_COPY, DROPEFFECT_LINK and/or |
| 69 | // DROPEFFECT_MOVE. |
| 70 | virtual DWORD OnDragOver(IDataObject* data_object, |
| 71 | DWORD key_state, |
| 72 | POINT cursor_position, |
| 73 | DWORD effect); |
| 74 | |
| 75 | // Invoked when the cursor moves outside the bounds of the hwnd during a |
| 76 | // dnd session. |
| 77 | virtual void OnDragLeave(IDataObject* data_object); |
| 78 | |
| 79 | // Invoked when the drop ends on the window. This should return the operation |
| 80 | // that was taken. |
| 81 | virtual DWORD OnDrop(IDataObject* data_object, |
| 82 | DWORD key_state, |
| 83 | POINT cursor_position, |
| 84 | DWORD effect); |
| 85 | |
| 86 | private: |
| 87 | // Returns the cached drop helper, creating one if necessary. The returned |
| 88 | // object is not addrefed. May return NULL if the object couldn't be created. |
| 89 | static IDropTargetHelper* DropHelper(); |
| 90 | |
| 91 | // The data object currently being dragged over this drop target. |
| 92 | CComPtr<IDataObject> current_data_object_; |
| 93 | |
| 94 | // A helper object that is used to provide drag image support while the mouse |
| 95 | // is dragging over the content area. |
| 96 | // |
| 97 | // DO NOT ACCESS DIRECTLY! Use DropHelper() instead, which will lazily create |
| 98 | // this if it doesn't exist yet. This object can take tens of milliseconds to |
| 99 | // create, and we don't want to block any window opening for this, especially |
| 100 | // since often, DnD will never be used. Instead, we force this penalty to the |
| 101 | // first time it is actually used. |
| 102 | static IDropTargetHelper* cached_drop_target_helper_; |
| 103 | |
| 104 | // The HWND of the source. This HWND is used to determine coordinates for |
| 105 | // mouse events that are sent to the renderer notifying various drag states. |
| 106 | HWND hwnd_; |
| 107 | |
| 108 | // Whether or not we are currently processing drag notifications for drags |
| 109 | // initiated in this window. |
| 110 | bool suspend_; |
| 111 | |
| 112 | LONG ref_count_; |
| 113 | |
| 114 | DISALLOW_EVIL_CONSTRUCTORS(BaseDropTarget); |
| 115 | }; |
| 116 | |
| 117 | #endif // BASE_BASE_DROP_TARGET_H__ |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 118 | |