nsScrollbarButtonFrame.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. //
  6. // Eric Vaughan
  7. // Netscape Communications
  8. //
  9. // See documentation in associated header file
  10. //
  11. #include "nsScrollbarButtonFrame.h"
  12. #include "nsPresContext.h"
  13. #include "nsIContent.h"
  14. #include "nsCOMPtr.h"
  15. #include "nsNameSpaceManager.h"
  16. #include "nsGkAtoms.h"
  17. #include "nsSliderFrame.h"
  18. #include "nsScrollbarFrame.h"
  19. #include "nsIScrollbarMediator.h"
  20. #include "nsRepeatService.h"
  21. #include "mozilla/LookAndFeel.h"
  22. #include "mozilla/MouseEvents.h"
  23. #include "mozilla/Telemetry.h"
  24. #include "mozilla/layers/ScrollInputMethods.h"
  25. using namespace mozilla;
  26. using mozilla::layers::ScrollInputMethod;
  27. //
  28. // NS_NewToolbarFrame
  29. //
  30. // Creates a new Toolbar frame and returns it
  31. //
  32. nsIFrame*
  33. NS_NewScrollbarButtonFrame (nsIPresShell* aPresShell, nsStyleContext* aContext)
  34. {
  35. return new (aPresShell) nsScrollbarButtonFrame(aContext);
  36. }
  37. NS_IMPL_FRAMEARENA_HELPERS(nsScrollbarButtonFrame)
  38. nsresult
  39. nsScrollbarButtonFrame::HandleEvent(nsPresContext* aPresContext,
  40. WidgetGUIEvent* aEvent,
  41. nsEventStatus* aEventStatus)
  42. {
  43. NS_ENSURE_ARG_POINTER(aEventStatus);
  44. // If a web page calls event.preventDefault() we still want to
  45. // scroll when scroll arrow is clicked. See bug 511075.
  46. if (!mContent->IsInNativeAnonymousSubtree() &&
  47. nsEventStatus_eConsumeNoDefault == *aEventStatus) {
  48. return NS_OK;
  49. }
  50. switch (aEvent->mMessage) {
  51. case eMouseDown:
  52. mCursorOnThis = true;
  53. // if we didn't handle the press ourselves, pass it on to the superclass
  54. if (HandleButtonPress(aPresContext, aEvent, aEventStatus)) {
  55. return NS_OK;
  56. }
  57. break;
  58. case eMouseUp:
  59. HandleRelease(aPresContext, aEvent, aEventStatus);
  60. break;
  61. case eMouseOut:
  62. mCursorOnThis = false;
  63. break;
  64. case eMouseMove: {
  65. nsPoint cursor =
  66. nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent, this);
  67. nsRect frameRect(nsPoint(0, 0), GetSize());
  68. mCursorOnThis = frameRect.Contains(cursor);
  69. break;
  70. }
  71. default:
  72. break;
  73. }
  74. return nsButtonBoxFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
  75. }
  76. bool
  77. nsScrollbarButtonFrame::HandleButtonPress(nsPresContext* aPresContext,
  78. WidgetGUIEvent* aEvent,
  79. nsEventStatus* aEventStatus)
  80. {
  81. // Get the desired action for the scrollbar button.
  82. LookAndFeel::IntID tmpAction;
  83. uint16_t button = aEvent->AsMouseEvent()->button;
  84. if (button == WidgetMouseEvent::eLeftButton) {
  85. tmpAction = LookAndFeel::eIntID_ScrollButtonLeftMouseButtonAction;
  86. } else if (button == WidgetMouseEvent::eMiddleButton) {
  87. tmpAction = LookAndFeel::eIntID_ScrollButtonMiddleMouseButtonAction;
  88. } else if (button == WidgetMouseEvent::eRightButton) {
  89. tmpAction = LookAndFeel::eIntID_ScrollButtonRightMouseButtonAction;
  90. } else {
  91. return false;
  92. }
  93. // Get the button action metric from the pres. shell.
  94. int32_t pressedButtonAction;
  95. if (NS_FAILED(LookAndFeel::GetInt(tmpAction, &pressedButtonAction))) {
  96. return false;
  97. }
  98. // get the scrollbar control
  99. nsIFrame* scrollbar;
  100. GetParentWithTag(nsGkAtoms::scrollbar, this, scrollbar);
  101. if (scrollbar == nullptr)
  102. return false;
  103. static nsIContent::AttrValuesArray strings[] = { &nsGkAtoms::increment,
  104. &nsGkAtoms::decrement,
  105. nullptr };
  106. int32_t index = mContent->FindAttrValueIn(kNameSpaceID_None,
  107. nsGkAtoms::type,
  108. strings, eCaseMatters);
  109. int32_t direction;
  110. if (index == 0)
  111. direction = 1;
  112. else if (index == 1)
  113. direction = -1;
  114. else
  115. return false;
  116. bool repeat = pressedButtonAction != 2;
  117. // set this attribute so we can style it later
  118. nsWeakFrame weakFrame(this);
  119. mContent->SetAttr(kNameSpaceID_None, nsGkAtoms::active, NS_LITERAL_STRING("true"), true);
  120. nsIPresShell::SetCapturingContent(mContent, CAPTURE_IGNOREALLOWED);
  121. if (!weakFrame.IsAlive()) {
  122. return false;
  123. }
  124. nsScrollbarFrame* sb = do_QueryFrame(scrollbar);
  125. if (sb) {
  126. nsIScrollbarMediator* m = sb->GetScrollbarMediator();
  127. switch (pressedButtonAction) {
  128. case 0:
  129. sb->SetIncrementToLine(direction);
  130. if (m) {
  131. m->ScrollByLine(sb, direction, nsIScrollbarMediator::ENABLE_SNAP);
  132. }
  133. break;
  134. case 1:
  135. sb->SetIncrementToPage(direction);
  136. if (m) {
  137. m->ScrollByPage(sb, direction, nsIScrollbarMediator::ENABLE_SNAP);
  138. }
  139. break;
  140. case 2:
  141. sb->SetIncrementToWhole(direction);
  142. if (m) {
  143. m->ScrollByWhole(sb, direction, nsIScrollbarMediator::ENABLE_SNAP);
  144. }
  145. break;
  146. case 3:
  147. default:
  148. // We were told to ignore this click, or someone assigned a non-standard
  149. // value to the button's action.
  150. return false;
  151. }
  152. if (!weakFrame.IsAlive()) {
  153. return false;
  154. }
  155. if (!m) {
  156. sb->MoveToNewPosition();
  157. if (!weakFrame.IsAlive()) {
  158. return false;
  159. }
  160. }
  161. }
  162. if (repeat) {
  163. StartRepeat();
  164. }
  165. return true;
  166. }
  167. NS_IMETHODIMP
  168. nsScrollbarButtonFrame::HandleRelease(nsPresContext* aPresContext,
  169. WidgetGUIEvent* aEvent,
  170. nsEventStatus* aEventStatus)
  171. {
  172. nsIPresShell::SetCapturingContent(nullptr, 0);
  173. // we're not active anymore
  174. mContent->UnsetAttr(kNameSpaceID_None, nsGkAtoms::active, true);
  175. StopRepeat();
  176. nsIFrame* scrollbar;
  177. GetParentWithTag(nsGkAtoms::scrollbar, this, scrollbar);
  178. nsScrollbarFrame* sb = do_QueryFrame(scrollbar);
  179. if (sb) {
  180. nsIScrollbarMediator* m = sb->GetScrollbarMediator();
  181. if (m) {
  182. m->ScrollbarReleased(sb);
  183. }
  184. }
  185. return NS_OK;
  186. }
  187. void nsScrollbarButtonFrame::Notify()
  188. {
  189. if (mCursorOnThis ||
  190. LookAndFeel::GetInt(
  191. LookAndFeel::eIntID_ScrollbarButtonAutoRepeatBehavior, 0)) {
  192. // get the scrollbar control
  193. nsIFrame* scrollbar;
  194. GetParentWithTag(nsGkAtoms::scrollbar, this, scrollbar);
  195. nsScrollbarFrame* sb = do_QueryFrame(scrollbar);
  196. if (sb) {
  197. nsIScrollbarMediator* m = sb->GetScrollbarMediator();
  198. if (m) {
  199. m->RepeatButtonScroll(sb);
  200. } else {
  201. sb->MoveToNewPosition();
  202. }
  203. }
  204. }
  205. }
  206. void
  207. nsScrollbarButtonFrame::MouseClicked(WidgetGUIEvent* aEvent)
  208. {
  209. nsButtonBoxFrame::MouseClicked(aEvent);
  210. //MouseClicked();
  211. }
  212. nsresult
  213. nsScrollbarButtonFrame::GetChildWithTag(nsIAtom* atom, nsIFrame* start,
  214. nsIFrame*& result)
  215. {
  216. // recursively search our children
  217. for (nsIFrame* childFrame : start->PrincipalChildList())
  218. {
  219. // get the content node
  220. nsIContent* child = childFrame->GetContent();
  221. if (child) {
  222. // see if it is the child
  223. if (child->IsXULElement(atom))
  224. {
  225. result = childFrame;
  226. return NS_OK;
  227. }
  228. }
  229. // recursive search the child
  230. GetChildWithTag(atom, childFrame, result);
  231. if (result != nullptr)
  232. return NS_OK;
  233. }
  234. result = nullptr;
  235. return NS_OK;
  236. }
  237. nsresult
  238. nsScrollbarButtonFrame::GetParentWithTag(nsIAtom* toFind, nsIFrame* start,
  239. nsIFrame*& result)
  240. {
  241. while (start)
  242. {
  243. start = start->GetParent();
  244. if (start) {
  245. // get the content node
  246. nsIContent* child = start->GetContent();
  247. if (child && child->IsXULElement(toFind)) {
  248. result = start;
  249. return NS_OK;
  250. }
  251. }
  252. }
  253. result = nullptr;
  254. return NS_OK;
  255. }
  256. void
  257. nsScrollbarButtonFrame::DestroyFrom(nsIFrame* aDestructRoot)
  258. {
  259. // Ensure our repeat service isn't going... it's possible that a scrollbar can disappear out
  260. // from under you while you're in the process of scrolling.
  261. StopRepeat();
  262. nsButtonBoxFrame::DestroyFrom(aDestructRoot);
  263. }