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