WebDropSource.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) 2007 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "WebDropSource.h"
  27. #include "WebKitDLL.h"
  28. #include "WebView.h"
  29. #include <WebCore/Cursor.h>
  30. #include <WebCore/DragActions.h>
  31. #include <WebCore/EventHandler.h>
  32. #include <WebCore/Frame.h>
  33. #include <WebCore/FrameView.h>
  34. #include <WebCore/Page.h>
  35. #include <WebCore/PlatformMouseEvent.h>
  36. #include <wtf/CurrentTime.h>
  37. #include <Windows.h>
  38. using namespace WebCore;
  39. HRESULT WebDropSource::createInstance(WebView* webView, IDropSource** result)
  40. {
  41. if (!webView || !result)
  42. return E_INVALIDARG;
  43. *result = new WebDropSource(webView);
  44. return S_OK;
  45. }
  46. WebDropSource::WebDropSource(WebView* webView)
  47. : m_ref(1)
  48. , m_dropped(false)
  49. , m_webView(webView)
  50. {
  51. gClassCount++;
  52. gClassNameCount.add("WebDropSource");
  53. }
  54. WebDropSource::~WebDropSource()
  55. {
  56. gClassCount--;
  57. gClassNameCount.remove("WebDropSource");
  58. }
  59. STDMETHODIMP WebDropSource::QueryInterface(REFIID riid, void** ppvObject)
  60. {
  61. *ppvObject = 0;
  62. if (IsEqualIID(riid, IID_IUnknown) ||
  63. IsEqualIID(riid, IID_IDropSource)) {
  64. *ppvObject = this;
  65. AddRef();
  66. return S_OK;
  67. }
  68. return E_NOINTERFACE;
  69. }
  70. STDMETHODIMP_(ULONG) WebDropSource::AddRef(void)
  71. {
  72. return InterlockedIncrement(&m_ref);
  73. }
  74. STDMETHODIMP_(ULONG) WebDropSource::Release(void)
  75. {
  76. long c = InterlockedDecrement(&m_ref);
  77. if (c == 0)
  78. delete this;
  79. return c;
  80. }
  81. PlatformMouseEvent generateMouseEvent(WebView* webView, bool isDrag)
  82. {
  83. POINTL pt;
  84. ::GetCursorPos((LPPOINT)&pt);
  85. POINTL localpt = pt;
  86. HWND viewWindow;
  87. if (SUCCEEDED(webView->viewWindow((OLE_HANDLE*)&viewWindow)))
  88. ::ScreenToClient(viewWindow, (LPPOINT)&localpt);
  89. return PlatformMouseEvent(IntPoint(localpt.x, localpt.y), IntPoint(pt.x, pt.y),
  90. isDrag ? LeftButton : NoButton, PlatformEvent::MouseMoved, 0, false, false, false, false, currentTime());
  91. }
  92. STDMETHODIMP WebDropSource::QueryContinueDrag(BOOL fEscapePressed, DWORD grfKeyState)
  93. {
  94. if (fEscapePressed || !(grfKeyState & (MK_LBUTTON|MK_RBUTTON))) {
  95. m_dropped = !fEscapePressed;
  96. return fEscapePressed? DRAGDROP_S_CANCEL : DRAGDROP_S_DROP;
  97. }
  98. return S_OK;
  99. }
  100. STDMETHODIMP WebDropSource::GiveFeedback(DWORD dwEffect)
  101. {
  102. BOOL showCustomCursors;
  103. if (FAILED(WebPreferences::sharedStandardPreferences()->customDragCursorsEnabled(&showCustomCursors)))
  104. return DRAGDROP_S_USEDEFAULTCURSORS;
  105. // If we don't want to hide the stop icon, let Windows select the cursor.
  106. if (!showCustomCursors)
  107. return DRAGDROP_S_USEDEFAULTCURSORS;
  108. // If we are going to show something other than the not allowed arrow, then let Windows
  109. // show the cursor.
  110. if (dwEffect != DROPEFFECT_NONE)
  111. return DRAGDROP_S_USEDEFAULTCURSORS;
  112. HWND viewWindow;
  113. if (FAILED(m_webView->viewWindow(reinterpret_cast<OLE_HANDLE*>(&viewWindow))))
  114. return DRAGDROP_S_USEDEFAULTCURSORS;
  115. RECT webViewRect;
  116. GetWindowRect(viewWindow, &webViewRect);
  117. POINT cursorPoint;
  118. GetCursorPos(&cursorPoint);
  119. if (!PtInRect(&webViewRect, cursorPoint)) {
  120. // If our cursor is outside the bounds of the webView, we want to let Windows select the cursor.
  121. return DRAGDROP_S_USEDEFAULTCURSORS;
  122. }
  123. FrameView* view = m_webView->page()->mainFrame()->view();
  124. if (!view)
  125. return DRAGDROP_S_USEDEFAULTCURSORS;
  126. // When dragging inside a WebView and the drag is not allowed, don't show the not allowed icon,
  127. // instead, show the pointer cursor.
  128. // FIXME <rdar://7577595>: Custom cursors aren't supported during drag and drop (default to pointer).
  129. view->setCursor(pointerCursor());
  130. return S_OK;
  131. }