EmbeddedWidget.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (C) 2008 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 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 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 "EmbeddedWidget.h"
  27. #include <WebCore/Document.h>
  28. #include <WebCore/FrameView.h>
  29. #include <WebCore/HTMLPlugInElement.h>
  30. #include <WebCore/RenderObject.h>
  31. #include "MemoryStream.h"
  32. #include "WebError.h"
  33. #include "WebURLResponse.h"
  34. using namespace WebCore;
  35. PassRefPtr<EmbeddedWidget> EmbeddedWidget::create(IWebEmbeddedView* view, HTMLPlugInElement* element, HWND parentWindow, const IntSize& size)
  36. {
  37. RefPtr<EmbeddedWidget> widget = adoptRef(new EmbeddedWidget(view, element));
  38. widget->createWindow(parentWindow, size);
  39. return widget.release();
  40. }
  41. EmbeddedWidget::~EmbeddedWidget()
  42. {
  43. if (m_window)
  44. DestroyWindow(m_window);
  45. }
  46. bool EmbeddedWidget::createWindow(HWND parentWindow, const IntSize& size)
  47. {
  48. ASSERT(!m_window);
  49. HWND window;
  50. SIZE pluginSize(size);
  51. HRESULT hr = m_view->createViewWindow((OLE_HANDLE)parentWindow, &pluginSize, (OLE_HANDLE*)&window);
  52. if (FAILED(hr) || !window)
  53. return false;
  54. m_window = window;
  55. return true;
  56. }
  57. void EmbeddedWidget::invalidateRect(const IntRect& rect)
  58. {
  59. if (!m_window)
  60. return;
  61. RECT r = rect;
  62. ::InvalidateRect(m_window, &r, false);
  63. }
  64. void EmbeddedWidget::setFrameRect(const IntRect& rect)
  65. {
  66. if (m_element->document()->printing())
  67. return;
  68. if (rect != frameRect())
  69. Widget::setFrameRect(rect);
  70. frameRectsChanged();
  71. }
  72. void EmbeddedWidget::frameRectsChanged()
  73. {
  74. if (!parent())
  75. return;
  76. ASSERT(parent()->isFrameView());
  77. FrameView* frameView = toFrameView(parent());
  78. IntRect oldWindowRect = m_windowRect;
  79. IntRect oldClipRect = m_clipRect;
  80. m_windowRect = IntRect(frameView->contentsToWindow(frameRect().location()), frameRect().size());
  81. m_clipRect = windowClipRect();
  82. m_clipRect.move(-m_windowRect.x(), -m_windowRect.y());
  83. if (!m_window)
  84. return;
  85. if (m_windowRect == oldWindowRect && m_clipRect == oldClipRect)
  86. return;
  87. HRGN rgn;
  88. // To prevent flashes while scrolling, we disable drawing during the window
  89. // update process by clipping the window to the zero rect.
  90. bool clipToZeroRect = true;
  91. if (clipToZeroRect) {
  92. rgn = ::CreateRectRgn(0, 0, 0, 0);
  93. ::SetWindowRgn(m_window, rgn, FALSE);
  94. } else {
  95. rgn = ::CreateRectRgn(m_clipRect.x(), m_clipRect.y(), m_clipRect.maxX(), m_clipRect.maxY());
  96. ::SetWindowRgn(m_window, rgn, TRUE);
  97. }
  98. if (m_windowRect != oldWindowRect)
  99. ::MoveWindow(m_window, m_windowRect.x(), m_windowRect.y(), m_windowRect.width(), m_windowRect.height(), TRUE);
  100. if (clipToZeroRect) {
  101. rgn = ::CreateRectRgn(m_clipRect.x(), m_clipRect.y(), m_clipRect.maxX(), m_clipRect.maxY());
  102. ::SetWindowRgn(m_window, rgn, TRUE);
  103. }
  104. }
  105. void EmbeddedWidget::setFocus(bool focused)
  106. {
  107. if (m_window && focused)
  108. SetFocus(m_window);
  109. Widget::setFocus(focused);
  110. }
  111. void EmbeddedWidget::show()
  112. {
  113. m_isVisible = true;
  114. if (m_attachedToWindow && m_window)
  115. ShowWindow(m_window, SW_SHOWNA);
  116. Widget::show();
  117. }
  118. void EmbeddedWidget::hide()
  119. {
  120. m_isVisible = false;
  121. if (m_attachedToWindow && m_window)
  122. ShowWindow(m_window, SW_HIDE);
  123. Widget::hide();
  124. }
  125. IntRect EmbeddedWidget::windowClipRect() const
  126. {
  127. // Start by clipping to our bounds.
  128. IntRect clipRect(m_windowRect);
  129. // Take our element and get the clip rect from the enclosing layer and frame view.
  130. FrameView* parentView = m_element->document()->view();
  131. clipRect.intersect(parentView->windowClipRectForFrameOwner(m_element, true));
  132. return clipRect;
  133. }
  134. void EmbeddedWidget::setParent(ScrollView* parent)
  135. {
  136. Widget::setParent(parent);
  137. if (!m_window)
  138. return;
  139. if (parent)
  140. return;
  141. // If the embedded window or one of its children have the focus, we need to
  142. // clear it to prevent the web view window from being focused because that can
  143. // trigger a layout while the plugin element is being detached.
  144. HWND focusedWindow = ::GetFocus();
  145. if (m_window == focusedWindow || ::IsChild(m_window, focusedWindow))
  146. ::SetFocus(0);
  147. }
  148. void EmbeddedWidget::attachToWindow()
  149. {
  150. if (m_attachedToWindow)
  151. return;
  152. m_attachedToWindow = true;
  153. if (m_isVisible && m_window)
  154. ShowWindow(m_window, SW_SHOWNA);
  155. }
  156. void EmbeddedWidget::detachFromWindow()
  157. {
  158. if (!m_attachedToWindow)
  159. return;
  160. if (m_isVisible && m_window)
  161. ShowWindow(m_window, SW_HIDE);
  162. m_attachedToWindow = false;
  163. }
  164. void EmbeddedWidget::didReceiveResponse(const ResourceResponse& response)
  165. {
  166. ASSERT(m_view);
  167. COMPtr<IWebURLResponse> urlResponse(AdoptCOM, WebURLResponse::createInstance(response));
  168. m_view->didReceiveResponse(urlResponse.get());
  169. }
  170. void EmbeddedWidget::didReceiveData(const char* data, int length)
  171. {
  172. COMPtr<MemoryStream> stream = MemoryStream::createInstance(SharedBuffer::create(data, length));
  173. m_view->didReceiveData(stream.get());
  174. }
  175. void EmbeddedWidget::didFinishLoading()
  176. {
  177. m_view->didFinishLoading();
  178. }
  179. void EmbeddedWidget::didFail(const ResourceError& error)
  180. {
  181. COMPtr<IWebError> webError(AdoptCOM, WebError::createInstance(error));
  182. m_view->didFail(webError.get());
  183. }