nsScrollBoxFrame.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #include "nsCOMPtr.h"
  6. #include "nsPresContext.h"
  7. #include "nsGkAtoms.h"
  8. #include "nsButtonBoxFrame.h"
  9. #include "nsITimer.h"
  10. #include "nsRepeatService.h"
  11. #include "mozilla/MouseEvents.h"
  12. #include "nsIContent.h"
  13. using namespace mozilla;
  14. class nsAutoRepeatBoxFrame : public nsButtonBoxFrame
  15. {
  16. public:
  17. NS_DECL_FRAMEARENA_HELPERS
  18. friend nsIFrame* NS_NewAutoRepeatBoxFrame(nsIPresShell* aPresShell,
  19. nsStyleContext* aContext);
  20. virtual void DestroyFrom(nsIFrame* aDestructRoot) override;
  21. virtual nsresult AttributeChanged(int32_t aNameSpaceID,
  22. nsIAtom* aAttribute,
  23. int32_t aModType) override;
  24. virtual nsresult HandleEvent(nsPresContext* aPresContext,
  25. WidgetGUIEvent* aEvent,
  26. nsEventStatus* aEventStatus) override;
  27. NS_IMETHOD HandlePress(nsPresContext* aPresContext,
  28. WidgetGUIEvent* aEvent,
  29. nsEventStatus* aEventStatus) override;
  30. NS_IMETHOD HandleRelease(nsPresContext* aPresContext,
  31. WidgetGUIEvent* aEvent,
  32. nsEventStatus* aEventStatus) override;
  33. protected:
  34. explicit nsAutoRepeatBoxFrame(nsStyleContext* aContext):
  35. nsButtonBoxFrame(aContext) {}
  36. void StartRepeat() {
  37. if (IsActivatedOnHover()) {
  38. // No initial delay on hover.
  39. nsRepeatService::GetInstance()->Start(Notify, this, 0);
  40. } else {
  41. nsRepeatService::GetInstance()->Start(Notify, this);
  42. }
  43. }
  44. void StopRepeat() {
  45. nsRepeatService::GetInstance()->Stop(Notify, this);
  46. }
  47. void Notify();
  48. static void Notify(void* aData) {
  49. static_cast<nsAutoRepeatBoxFrame*>(aData)->Notify();
  50. }
  51. bool mTrustedEvent;
  52. bool IsActivatedOnHover();
  53. };
  54. nsIFrame*
  55. NS_NewAutoRepeatBoxFrame (nsIPresShell* aPresShell, nsStyleContext* aContext)
  56. {
  57. return new (aPresShell) nsAutoRepeatBoxFrame(aContext);
  58. }
  59. NS_IMPL_FRAMEARENA_HELPERS(nsAutoRepeatBoxFrame)
  60. nsresult
  61. nsAutoRepeatBoxFrame::HandleEvent(nsPresContext* aPresContext,
  62. WidgetGUIEvent* aEvent,
  63. nsEventStatus* aEventStatus)
  64. {
  65. NS_ENSURE_ARG_POINTER(aEventStatus);
  66. if (nsEventStatus_eConsumeNoDefault == *aEventStatus) {
  67. return NS_OK;
  68. }
  69. switch(aEvent->mMessage) {
  70. // repeat mode may be "hover" for repeating while the mouse is hovering
  71. // over the element, otherwise repetition is done while the element is
  72. // active (pressed).
  73. case eMouseEnterIntoWidget:
  74. case eMouseOver:
  75. if (IsActivatedOnHover()) {
  76. StartRepeat();
  77. mTrustedEvent = aEvent->IsTrusted();
  78. }
  79. break;
  80. case eMouseExitFromWidget:
  81. case eMouseOut:
  82. // always stop on mouse exit
  83. StopRepeat();
  84. // Not really necessary but do this to be safe
  85. mTrustedEvent = false;
  86. break;
  87. case eMouseClick: {
  88. WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent();
  89. if (mouseEvent->IsLeftClickEvent()) {
  90. // skip button frame handling to prevent click handling
  91. return nsBoxFrame::HandleEvent(aPresContext, mouseEvent, aEventStatus);
  92. }
  93. break;
  94. }
  95. default:
  96. break;
  97. }
  98. return nsButtonBoxFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
  99. }
  100. NS_IMETHODIMP
  101. nsAutoRepeatBoxFrame::HandlePress(nsPresContext* aPresContext,
  102. WidgetGUIEvent* aEvent,
  103. nsEventStatus* aEventStatus)
  104. {
  105. if (!IsActivatedOnHover()) {
  106. StartRepeat();
  107. mTrustedEvent = aEvent->IsTrusted();
  108. DoMouseClick(aEvent, mTrustedEvent);
  109. }
  110. return NS_OK;
  111. }
  112. NS_IMETHODIMP
  113. nsAutoRepeatBoxFrame::HandleRelease(nsPresContext* aPresContext,
  114. WidgetGUIEvent* aEvent,
  115. nsEventStatus* aEventStatus)
  116. {
  117. if (!IsActivatedOnHover()) {
  118. StopRepeat();
  119. }
  120. return NS_OK;
  121. }
  122. nsresult
  123. nsAutoRepeatBoxFrame::AttributeChanged(int32_t aNameSpaceID,
  124. nsIAtom* aAttribute,
  125. int32_t aModType)
  126. {
  127. if (aAttribute == nsGkAtoms::type) {
  128. StopRepeat();
  129. }
  130. return NS_OK;
  131. }
  132. void
  133. nsAutoRepeatBoxFrame::Notify()
  134. {
  135. DoMouseClick(nullptr, mTrustedEvent);
  136. }
  137. void
  138. nsAutoRepeatBoxFrame::DestroyFrom(nsIFrame* aDestructRoot)
  139. {
  140. // Ensure our repeat service isn't going... it's possible that a scrollbar can disappear out
  141. // from under you while you're in the process of scrolling.
  142. StopRepeat();
  143. nsButtonBoxFrame::DestroyFrom(aDestructRoot);
  144. }
  145. bool
  146. nsAutoRepeatBoxFrame::IsActivatedOnHover()
  147. {
  148. return mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::repeat,
  149. nsGkAtoms::hover, eCaseMatters);
  150. }