WebScrollBar.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 "WebKitDLL.h"
  27. #include "WebKit.h"
  28. #include "WebScrollBar.h"
  29. #include <WebCore/GraphicsContext.h>
  30. #include <WebCore/PlatformMouseEvent.h>
  31. #include <WebCore/Scrollbar.h>
  32. #include <WebCore/ScrollbarTheme.h>
  33. using namespace WebCore;
  34. // WebScrollBar ---------------------------------------------------------------------
  35. WebScrollBar::WebScrollBar()
  36. : m_refCount(0)
  37. , m_containingWindow(0)
  38. , m_currentPosition(0)
  39. {
  40. gClassCount++;
  41. gClassNameCount.add("WebScrollBar");
  42. }
  43. WebScrollBar::~WebScrollBar()
  44. {
  45. gClassCount--;
  46. gClassNameCount.remove("WebScrollBar");
  47. }
  48. WebScrollBar* WebScrollBar::createInstance()
  49. {
  50. WebScrollBar* instance = new WebScrollBar();
  51. instance->AddRef();
  52. return instance;
  53. }
  54. // IUnknown -------------------------------------------------------------------
  55. HRESULT STDMETHODCALLTYPE WebScrollBar::QueryInterface(REFIID riid, void** ppvObject)
  56. {
  57. *ppvObject = 0;
  58. if (IsEqualGUID(riid, IID_IUnknown))
  59. *ppvObject = static_cast<IUnknown*>(this);
  60. else if (IsEqualGUID(riid, IID_IWebScrollBarPrivate))
  61. *ppvObject = static_cast<IWebScrollBarPrivate*>(this);
  62. else
  63. return E_NOINTERFACE;
  64. AddRef();
  65. return S_OK;
  66. }
  67. ULONG STDMETHODCALLTYPE WebScrollBar::AddRef(void)
  68. {
  69. return ++m_refCount;
  70. }
  71. ULONG STDMETHODCALLTYPE WebScrollBar::Release(void)
  72. {
  73. ULONG newRef = --m_refCount;
  74. if (!newRef)
  75. delete(this);
  76. return newRef;
  77. }
  78. // IWebScrollBarPrivate ------------------------------------------------------------------
  79. HRESULT STDMETHODCALLTYPE WebScrollBar::init(
  80. /* [in] */ IWebScrollBarDelegatePrivate* delegate,
  81. /* [in] */ OLE_HANDLE containingWindow,
  82. /* [in] */ WebScrollBarOrientation orientation,
  83. /* [in] */ WebScrollBarControlSize controlSize)
  84. {
  85. if (!delegate || !containingWindow)
  86. return E_FAIL;
  87. ScrollbarOrientation webCoreOrientation = (ScrollbarOrientation) orientation;
  88. ScrollbarControlSize webCoreControlSize = (ScrollbarControlSize) controlSize;
  89. m_delegate = delegate;
  90. m_scrollBar = Scrollbar::createNativeScrollbar(this, webCoreOrientation, webCoreControlSize);
  91. if (!m_scrollBar)
  92. return E_FAIL;
  93. m_containingWindow = (HWND)(ULONG64)containingWindow;
  94. return S_OK;
  95. }
  96. HRESULT STDMETHODCALLTYPE WebScrollBar::setEnabled(
  97. /* [in] */ BOOL enabled)
  98. {
  99. m_scrollBar->setEnabled(!!enabled);
  100. return S_OK;
  101. }
  102. HRESULT STDMETHODCALLTYPE WebScrollBar::setSteps(
  103. /* [in] */ int lineStep,
  104. /* [in] */ int pageStep)
  105. {
  106. m_scrollBar->setSteps(lineStep, pageStep);
  107. return S_OK;
  108. }
  109. HRESULT STDMETHODCALLTYPE WebScrollBar::setProportion(
  110. /* [in] */ int visibleSize,
  111. /* [in] */ int totalSize)
  112. {
  113. m_scrollBar->setProportion(visibleSize, totalSize);
  114. return S_OK;
  115. }
  116. HRESULT STDMETHODCALLTYPE WebScrollBar::setRect(
  117. /* [in] */ RECT bounds)
  118. {
  119. IntRect rect(bounds.left, bounds.top, bounds.right-bounds.left, bounds.bottom-bounds.top);
  120. m_scrollBar->setFrameRect(rect);
  121. return S_OK;
  122. }
  123. HRESULT STDMETHODCALLTYPE WebScrollBar::setValue(
  124. /* [in] */ int value)
  125. {
  126. m_currentPosition = value;
  127. ScrollableArea::scrollToOffsetWithoutAnimation(m_scrollBar->orientation(), m_currentPosition);
  128. return S_OK;
  129. }
  130. HRESULT STDMETHODCALLTYPE WebScrollBar::value(
  131. /* [retval][out] */ int* value)
  132. {
  133. if (!value)
  134. return E_POINTER;
  135. *value = m_currentPosition;
  136. return S_OK;
  137. }
  138. HRESULT STDMETHODCALLTYPE WebScrollBar::paint(
  139. /* [in] */ HDC dc,
  140. /* [in] */ RECT damageRect)
  141. {
  142. GraphicsContext context(dc);
  143. IntRect rect(damageRect.left, damageRect.top, damageRect.right-damageRect.left, damageRect.bottom-damageRect.top);
  144. m_scrollBar->paint(&context, rect);
  145. return S_OK;
  146. }
  147. HRESULT STDMETHODCALLTYPE WebScrollBar::frameRect(
  148. /* [retval][out] */ RECT* bounds)
  149. {
  150. if (!bounds)
  151. return E_POINTER;
  152. IntRect rect = m_scrollBar->frameRect();
  153. bounds->left = rect.x();
  154. bounds->right = rect.maxX();
  155. bounds->top = rect.y();
  156. bounds->bottom = rect.maxY();
  157. return S_OK;
  158. }
  159. HRESULT STDMETHODCALLTYPE WebScrollBar::width(
  160. /* [retval][out] */ int* w)
  161. {
  162. if (!w)
  163. return E_POINTER;
  164. *w = m_scrollBar->width();
  165. return S_OK;
  166. }
  167. HRESULT STDMETHODCALLTYPE WebScrollBar::height(
  168. /* [retval][out] */ int* h)
  169. {
  170. if (!h)
  171. return E_POINTER;
  172. *h = m_scrollBar->height();
  173. return S_OK;
  174. }
  175. HRESULT STDMETHODCALLTYPE WebScrollBar::requestedWidth(
  176. /* [retval][out] */ int* w)
  177. {
  178. if (!w)
  179. return E_POINTER;
  180. *w = m_scrollBar->orientation() == VerticalScrollbar ? ScrollbarTheme::theme()->scrollbarThickness(m_scrollBar->controlSize()) : -1;
  181. return S_OK;
  182. }
  183. HRESULT STDMETHODCALLTYPE WebScrollBar::requestedHeight(
  184. /* [retval][out] */ int* h)
  185. {
  186. if (!h)
  187. return E_POINTER;
  188. *h = m_scrollBar->orientation() == HorizontalScrollbar ? ScrollbarTheme::theme()->scrollbarThickness(m_scrollBar->controlSize()) : -1;
  189. return S_OK;
  190. }
  191. HRESULT STDMETHODCALLTYPE WebScrollBar::handleMouseEvent(
  192. OLE_HANDLE window,
  193. UINT msg,
  194. WPARAM wParam,
  195. LPARAM lParam)
  196. {
  197. PlatformMouseEvent mouseEvent((HWND)(ULONG64)window, msg, wParam, lParam);
  198. switch (msg) {
  199. case WM_LBUTTONDOWN:
  200. m_scrollBar->mouseDown(mouseEvent);
  201. break;
  202. case WM_LBUTTONUP:
  203. m_scrollBar->mouseUp(mouseEvent);
  204. break;
  205. case WM_MOUSEMOVE:
  206. m_scrollBar->mouseMoved(mouseEvent);
  207. break;
  208. }
  209. return S_OK;
  210. }
  211. HRESULT STDMETHODCALLTYPE WebScrollBar::scroll(
  212. WebScrollDirection direction,
  213. WebScrollGranularity granularity,
  214. float multiplier)
  215. {
  216. ScrollDirection webCoreScrollDirection = (ScrollDirection) direction;
  217. ScrollGranularity webCoreGranularity = (ScrollGranularity) granularity;
  218. ScrollableArea::scroll(webCoreScrollDirection, webCoreGranularity, multiplier);
  219. return S_OK;
  220. }
  221. // ScrollableArea -------------------------------------------------------
  222. int WebScrollBar::scrollSize(ScrollbarOrientation orientation) const
  223. {
  224. return (orientation == m_scrollBar->orientation()) ? (m_scrollBar->totalSize() - m_scrollBar->visibleSize()) : 0;
  225. }
  226. int WebScrollBar::scrollPosition(Scrollbar*) const
  227. {
  228. return m_currentPosition;
  229. }
  230. void WebScrollBar::setScrollOffset(const IntPoint& offset)
  231. {
  232. m_currentPosition = (m_scrollBar->orientation() == HorizontalScrollbar) ? offset.x() : offset.y();
  233. m_delegate->valueChanged(this);
  234. }
  235. void WebScrollBar::invalidateScrollbarRect(Scrollbar*, const IntRect& rect)
  236. {
  237. RECT r = rect;
  238. ::InvalidateRect(m_containingWindow, &r, false);
  239. }
  240. Scrollbar* WebScrollBar::horizontalScrollbar() const
  241. {
  242. return m_scrollBar->orientation() == HorizontalScrollbar ? m_scrollBar.get() : 0;
  243. }
  244. Scrollbar* WebScrollBar::verticalScrollbar() const
  245. {
  246. return m_scrollBar->orientation() == VerticalScrollbar ? m_scrollBar.get() : 0;
  247. }
  248. int WebScrollBar::visibleHeight() const
  249. {
  250. return m_scrollBar->height();
  251. }
  252. int WebScrollBar::visibleWidth() const
  253. {
  254. return m_scrollBar->width();
  255. }
  256. WebCore::IntSize WebScrollBar::contentsSize() const
  257. {
  258. return m_scrollBar->frameRect().size();
  259. }
  260. bool WebScrollBar::scrollbarsCanBeActive() const
  261. {
  262. return true;
  263. }
  264. WebCore::IntRect WebScrollBar::scrollableAreaBoundingBox() const
  265. {
  266. return m_scrollBar->frameRect();
  267. }