WKBundlePageOverlay.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) 2010 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. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "WKBundlePageOverlay.h"
  27. #include "PageOverlay.h"
  28. #include "WKAPICast.h"
  29. #include "WKBundleAPICast.h"
  30. #include <WebCore/GraphicsContext.h>
  31. #include <wtf/PassOwnPtr.h>
  32. using namespace WebCore;
  33. using namespace WebKit;
  34. class PageOverlayClientImpl : public PageOverlay::Client {
  35. public:
  36. static PassOwnPtr<PageOverlayClientImpl> create(WKBundlePageOverlayClient* client)
  37. {
  38. return adoptPtr(new PageOverlayClientImpl(client));
  39. }
  40. virtual void setAccessibilityClient(WKBundlePageOverlayAccessibilityClient* client)
  41. {
  42. if (client)
  43. m_accessibilityClient = *client;
  44. }
  45. private:
  46. explicit PageOverlayClientImpl(WKBundlePageOverlayClient* client)
  47. : m_client()
  48. , m_accessibilityClient()
  49. {
  50. if (client)
  51. m_client = *client;
  52. }
  53. // PageOverlay::Client.
  54. virtual void pageOverlayDestroyed(PageOverlay*)
  55. {
  56. delete this;
  57. }
  58. virtual void willMoveToWebPage(PageOverlay* pageOverlay, WebPage* page)
  59. {
  60. if (!m_client.willMoveToPage)
  61. return;
  62. m_client.willMoveToPage(toAPI(pageOverlay), toAPI(page), m_client.clientInfo);
  63. }
  64. virtual void didMoveToWebPage(PageOverlay* pageOverlay, WebPage* page)
  65. {
  66. if (!m_client.didMoveToPage)
  67. return;
  68. m_client.didMoveToPage(toAPI(pageOverlay), toAPI(page), m_client.clientInfo);
  69. }
  70. virtual void drawRect(PageOverlay* pageOverlay, GraphicsContext& graphicsContext, const IntRect& dirtyRect)
  71. {
  72. if (!m_client.drawRect)
  73. return;
  74. m_client.drawRect(toAPI(pageOverlay), graphicsContext.platformContext(), toAPI(dirtyRect), m_client.clientInfo);
  75. }
  76. virtual bool mouseEvent(PageOverlay* pageOverlay, const WebMouseEvent& event)
  77. {
  78. switch (event.type()) {
  79. case WebEvent::MouseDown: {
  80. if (!m_client.mouseDown)
  81. return false;
  82. return m_client.mouseDown(toAPI(pageOverlay), toAPI(event.position()), toAPI(event.button()), m_client.clientInfo);
  83. }
  84. case WebEvent::MouseUp: {
  85. if (!m_client.mouseUp)
  86. return false;
  87. return m_client.mouseUp(toAPI(pageOverlay), toAPI(event.position()), toAPI(event.button()), m_client.clientInfo);
  88. }
  89. case WebEvent::MouseMove: {
  90. if (event.button() == WebMouseEvent::NoButton) {
  91. if (!m_client.mouseMoved)
  92. return false;
  93. return m_client.mouseMoved(toAPI(pageOverlay), toAPI(event.position()), m_client.clientInfo);
  94. }
  95. // This is a MouseMove event with a mouse button pressed. Call mouseDragged.
  96. if (!m_client.mouseDragged)
  97. return false;
  98. return m_client.mouseDragged(toAPI(pageOverlay), toAPI(event.position()), toAPI(event.button()), m_client.clientInfo);
  99. }
  100. default:
  101. return false;
  102. }
  103. }
  104. virtual WKTypeRef copyAccessibilityAttributeValue(PageOverlay* pageOverlay, WKStringRef attribute, WKTypeRef parameter)
  105. {
  106. if (!m_accessibilityClient.copyAccessibilityAttributeValue)
  107. return 0;
  108. return m_accessibilityClient.copyAccessibilityAttributeValue(toAPI(pageOverlay), attribute, parameter, m_client.clientInfo);
  109. }
  110. virtual WKArrayRef copyAccessibilityAttributeNames(PageOverlay* pageOverlay, bool paramerizedNames)
  111. {
  112. if (!m_accessibilityClient.copyAccessibilityAttributeNames)
  113. return 0;
  114. return m_accessibilityClient.copyAccessibilityAttributeNames(toAPI(pageOverlay), paramerizedNames, m_client.clientInfo);
  115. }
  116. WKBundlePageOverlayClient m_client;
  117. WKBundlePageOverlayAccessibilityClient m_accessibilityClient;
  118. };
  119. WKTypeID WKBundlePageOverlayGetTypeID()
  120. {
  121. return toAPI(PageOverlay::APIType);
  122. }
  123. WKBundlePageOverlayRef WKBundlePageOverlayCreate(WKBundlePageOverlayClient* wkClient)
  124. {
  125. if (wkClient && wkClient->version)
  126. return 0;
  127. OwnPtr<PageOverlayClientImpl> clientImpl = PageOverlayClientImpl::create(wkClient);
  128. return toAPI(PageOverlay::create(clientImpl.leakPtr()).leakRef());
  129. }
  130. void WKBundlePageOverlaySetAccessibilityClient(WKBundlePageOverlayRef bundlePageOverlayRef, WKBundlePageOverlayAccessibilityClient* client)
  131. {
  132. if (client && client->version)
  133. return;
  134. static_cast<PageOverlayClientImpl*>(toImpl(bundlePageOverlayRef)->client())->setAccessibilityClient(client);
  135. }
  136. void WKBundlePageOverlaySetNeedsDisplay(WKBundlePageOverlayRef bundlePageOverlayRef, WKRect rect)
  137. {
  138. toImpl(bundlePageOverlayRef)->setNeedsDisplay(enclosingIntRect(toFloatRect(rect)));
  139. }
  140. float WKBundlePageOverlayFractionFadedIn(WKBundlePageOverlayRef bundlePageOverlayRef)
  141. {
  142. return toImpl(bundlePageOverlayRef)->fractionFadedIn();
  143. }