nsSliderFrame.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  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 "nsSliderFrame.h"
  12. #include "gfxPrefs.h"
  13. #include "nsStyleContext.h"
  14. #include "nsPresContext.h"
  15. #include "nsIContent.h"
  16. #include "nsCOMPtr.h"
  17. #include "nsNameSpaceManager.h"
  18. #include "nsGkAtoms.h"
  19. #include "nsHTMLParts.h"
  20. #include "nsIPresShell.h"
  21. #include "nsCSSRendering.h"
  22. #include "nsIDOMEvent.h"
  23. #include "nsIDOMMouseEvent.h"
  24. #include "nsScrollbarButtonFrame.h"
  25. #include "nsISliderListener.h"
  26. #include "nsIScrollableFrame.h"
  27. #include "nsIScrollbarMediator.h"
  28. #include "nsISupportsImpl.h"
  29. #include "nsScrollbarFrame.h"
  30. #include "nsRepeatService.h"
  31. #include "nsBoxLayoutState.h"
  32. #include "nsSprocketLayout.h"
  33. #include "nsIServiceManager.h"
  34. #include "nsContentUtils.h"
  35. #include "nsLayoutUtils.h"
  36. #include "nsDisplayList.h"
  37. #include "mozilla/Assertions.h" // for MOZ_ASSERT
  38. #include "mozilla/Preferences.h"
  39. #include "mozilla/LookAndFeel.h"
  40. #include "mozilla/MouseEvents.h"
  41. #include "mozilla/Telemetry.h"
  42. #include "mozilla/layers/APZCCallbackHelper.h"
  43. #include "mozilla/layers/AsyncDragMetrics.h"
  44. #include "mozilla/layers/InputAPZContext.h"
  45. #include "mozilla/layers/ScrollInputMethods.h"
  46. #include <algorithm>
  47. using namespace mozilla;
  48. using mozilla::layers::APZCCallbackHelper;
  49. using mozilla::layers::AsyncDragMetrics;
  50. using mozilla::layers::InputAPZContext;
  51. using mozilla::layers::ScrollInputMethod;
  52. bool nsSliderFrame::gMiddlePref = false;
  53. int32_t nsSliderFrame::gSnapMultiplier;
  54. // Turn this on if you want to debug slider frames.
  55. #undef DEBUG_SLIDER
  56. static already_AddRefed<nsIContent>
  57. GetContentOfBox(nsIFrame *aBox)
  58. {
  59. nsCOMPtr<nsIContent> content = aBox->GetContent();
  60. return content.forget();
  61. }
  62. nsIFrame*
  63. NS_NewSliderFrame (nsIPresShell* aPresShell, nsStyleContext* aContext)
  64. {
  65. return new (aPresShell) nsSliderFrame(aContext);
  66. }
  67. NS_IMPL_FRAMEARENA_HELPERS(nsSliderFrame)
  68. NS_QUERYFRAME_HEAD(nsSliderFrame)
  69. NS_QUERYFRAME_ENTRY(nsSliderFrame)
  70. NS_QUERYFRAME_TAIL_INHERITING(nsBoxFrame)
  71. nsSliderFrame::nsSliderFrame(nsStyleContext* aContext):
  72. nsBoxFrame(aContext),
  73. mCurPos(0),
  74. mChange(0),
  75. mDragFinished(true),
  76. mUserChanged(false),
  77. mScrollingWithAPZ(false),
  78. mSuppressionActive(false)
  79. {
  80. }
  81. // stop timer
  82. nsSliderFrame::~nsSliderFrame()
  83. {
  84. if (mSuppressionActive) {
  85. APZCCallbackHelper::SuppressDisplayport(false, PresContext() ?
  86. PresContext()->PresShell() :
  87. nullptr);
  88. }
  89. }
  90. void
  91. nsSliderFrame::Init(nsIContent* aContent,
  92. nsContainerFrame* aParent,
  93. nsIFrame* aPrevInFlow)
  94. {
  95. nsBoxFrame::Init(aContent, aParent, aPrevInFlow);
  96. static bool gotPrefs = false;
  97. if (!gotPrefs) {
  98. gotPrefs = true;
  99. gMiddlePref = Preferences::GetBool("middlemouse.scrollbarPosition");
  100. gSnapMultiplier = Preferences::GetInt("slider.snapMultiplier");
  101. }
  102. mCurPos = GetCurrentPosition(aContent);
  103. }
  104. void
  105. nsSliderFrame::RemoveFrame(ChildListID aListID,
  106. nsIFrame* aOldFrame)
  107. {
  108. nsBoxFrame::RemoveFrame(aListID, aOldFrame);
  109. if (mFrames.IsEmpty())
  110. RemoveListener();
  111. }
  112. void
  113. nsSliderFrame::InsertFrames(ChildListID aListID,
  114. nsIFrame* aPrevFrame,
  115. nsFrameList& aFrameList)
  116. {
  117. bool wasEmpty = mFrames.IsEmpty();
  118. nsBoxFrame::InsertFrames(aListID, aPrevFrame, aFrameList);
  119. if (wasEmpty)
  120. AddListener();
  121. }
  122. void
  123. nsSliderFrame::AppendFrames(ChildListID aListID,
  124. nsFrameList& aFrameList)
  125. {
  126. // if we have no children and on was added then make sure we add the
  127. // listener
  128. bool wasEmpty = mFrames.IsEmpty();
  129. nsBoxFrame::AppendFrames(aListID, aFrameList);
  130. if (wasEmpty)
  131. AddListener();
  132. }
  133. int32_t
  134. nsSliderFrame::GetCurrentPosition(nsIContent* content)
  135. {
  136. return GetIntegerAttribute(content, nsGkAtoms::curpos, 0);
  137. }
  138. int32_t
  139. nsSliderFrame::GetMinPosition(nsIContent* content)
  140. {
  141. return GetIntegerAttribute(content, nsGkAtoms::minpos, 0);
  142. }
  143. int32_t
  144. nsSliderFrame::GetMaxPosition(nsIContent* content)
  145. {
  146. return GetIntegerAttribute(content, nsGkAtoms::maxpos, 100);
  147. }
  148. int32_t
  149. nsSliderFrame::GetIncrement(nsIContent* content)
  150. {
  151. return GetIntegerAttribute(content, nsGkAtoms::increment, 1);
  152. }
  153. int32_t
  154. nsSliderFrame::GetPageIncrement(nsIContent* content)
  155. {
  156. return GetIntegerAttribute(content, nsGkAtoms::pageincrement, 10);
  157. }
  158. int32_t
  159. nsSliderFrame::GetIntegerAttribute(nsIContent* content, nsIAtom* atom, int32_t defaultValue)
  160. {
  161. nsAutoString value;
  162. content->GetAttr(kNameSpaceID_None, atom, value);
  163. if (!value.IsEmpty()) {
  164. nsresult error;
  165. // convert it to an integer
  166. defaultValue = value.ToInteger(&error);
  167. }
  168. return defaultValue;
  169. }
  170. class nsValueChangedRunnable : public Runnable
  171. {
  172. public:
  173. nsValueChangedRunnable(nsISliderListener* aListener,
  174. nsIAtom* aWhich,
  175. int32_t aValue,
  176. bool aUserChanged)
  177. : mListener(aListener), mWhich(aWhich),
  178. mValue(aValue), mUserChanged(aUserChanged)
  179. {}
  180. NS_IMETHOD Run() override
  181. {
  182. return mListener->ValueChanged(nsDependentAtomString(mWhich),
  183. mValue, mUserChanged);
  184. }
  185. nsCOMPtr<nsISliderListener> mListener;
  186. nsCOMPtr<nsIAtom> mWhich;
  187. int32_t mValue;
  188. bool mUserChanged;
  189. };
  190. class nsDragStateChangedRunnable : public Runnable
  191. {
  192. public:
  193. nsDragStateChangedRunnable(nsISliderListener* aListener,
  194. bool aDragBeginning)
  195. : mListener(aListener),
  196. mDragBeginning(aDragBeginning)
  197. {}
  198. NS_IMETHOD Run() override
  199. {
  200. return mListener->DragStateChanged(mDragBeginning);
  201. }
  202. nsCOMPtr<nsISliderListener> mListener;
  203. bool mDragBeginning;
  204. };
  205. nsresult
  206. nsSliderFrame::AttributeChanged(int32_t aNameSpaceID,
  207. nsIAtom* aAttribute,
  208. int32_t aModType)
  209. {
  210. nsresult rv = nsBoxFrame::AttributeChanged(aNameSpaceID, aAttribute,
  211. aModType);
  212. // if the current position changes
  213. if (aAttribute == nsGkAtoms::curpos) {
  214. CurrentPositionChanged();
  215. } else if (aAttribute == nsGkAtoms::minpos ||
  216. aAttribute == nsGkAtoms::maxpos) {
  217. // bounds check it.
  218. nsIFrame* scrollbarBox = GetScrollbar();
  219. nsCOMPtr<nsIContent> scrollbar;
  220. scrollbar = GetContentOfBox(scrollbarBox);
  221. int32_t current = GetCurrentPosition(scrollbar);
  222. int32_t min = GetMinPosition(scrollbar);
  223. int32_t max = GetMaxPosition(scrollbar);
  224. // inform the parent <scale> that the minimum or maximum changed
  225. nsIFrame* parent = GetParent();
  226. if (parent) {
  227. nsCOMPtr<nsISliderListener> sliderListener = do_QueryInterface(parent->GetContent());
  228. if (sliderListener) {
  229. nsContentUtils::AddScriptRunner(
  230. new nsValueChangedRunnable(sliderListener, aAttribute,
  231. aAttribute == nsGkAtoms::minpos ? min : max, false));
  232. }
  233. }
  234. if (current < min || current > max)
  235. {
  236. int32_t direction = 0;
  237. if (current < min || max < min) {
  238. current = min;
  239. direction = -1;
  240. } else if (current > max) {
  241. current = max;
  242. direction = 1;
  243. }
  244. // set the new position and notify observers
  245. nsScrollbarFrame* scrollbarFrame = do_QueryFrame(scrollbarBox);
  246. if (scrollbarFrame) {
  247. nsIScrollbarMediator* mediator = scrollbarFrame->GetScrollbarMediator();
  248. scrollbarFrame->SetIncrementToWhole(direction);
  249. if (mediator) {
  250. mediator->ScrollByWhole(scrollbarFrame, direction,
  251. nsIScrollbarMediator::ENABLE_SNAP);
  252. }
  253. }
  254. // 'this' might be destroyed here
  255. nsContentUtils::AddScriptRunner(
  256. new nsSetAttrRunnable(scrollbar, nsGkAtoms::curpos, current));
  257. }
  258. }
  259. if (aAttribute == nsGkAtoms::minpos ||
  260. aAttribute == nsGkAtoms::maxpos ||
  261. aAttribute == nsGkAtoms::pageincrement ||
  262. aAttribute == nsGkAtoms::increment) {
  263. PresContext()->PresShell()->
  264. FrameNeedsReflow(this, nsIPresShell::eStyleChange, NS_FRAME_IS_DIRTY);
  265. }
  266. return rv;
  267. }
  268. void
  269. nsSliderFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
  270. const nsDisplayListSet& aLists)
  271. {
  272. if (aBuilder->IsForEventDelivery() && isDraggingThumb()) {
  273. // This is EVIL, we shouldn't be messing with event delivery just to get
  274. // thumb mouse drag events to arrive at the slider!
  275. aLists.Outlines()->AppendNewToTop(new (aBuilder)
  276. nsDisplayEventReceiver(aBuilder, this));
  277. return;
  278. }
  279. nsBoxFrame::BuildDisplayList(aBuilder, aLists);
  280. }
  281. void
  282. nsSliderFrame::BuildDisplayListForChildren(nsDisplayListBuilder* aBuilder,
  283. const nsDisplayListSet& aLists)
  284. {
  285. // if we are too small to have a thumb don't paint it.
  286. nsIFrame* thumb = nsBox::GetChildXULBox(this);
  287. if (thumb) {
  288. nsRect thumbRect(thumb->GetRect());
  289. nsMargin m;
  290. thumb->GetXULMargin(m);
  291. thumbRect.Inflate(m);
  292. nsRect crect;
  293. GetXULClientRect(crect);
  294. if (crect.width < thumbRect.width || crect.height < thumbRect.height)
  295. return;
  296. // If this scrollbar is the scrollbar of an actively scrolled scroll frame,
  297. // layerize the scrollbar thumb, wrap it in its own ContainerLayer and
  298. // attach scrolling information to it.
  299. // We do this here and not in the thumb's nsBoxFrame::BuildDisplayList so
  300. // that the event region that gets created for the thumb is included in
  301. // the nsDisplayOwnLayer contents.
  302. uint32_t flags = 0;
  303. mozilla::layers::FrameMetrics::ViewID scrollTargetId =
  304. mozilla::layers::FrameMetrics::NULL_SCROLL_ID;
  305. aBuilder->GetScrollbarInfo(&scrollTargetId, &flags);
  306. bool thumbGetsLayer = (scrollTargetId != layers::FrameMetrics::NULL_SCROLL_ID);
  307. nsLayoutUtils::SetScrollbarThumbLayerization(thumb, thumbGetsLayer);
  308. if (thumbGetsLayer) {
  309. nsDisplayListCollection tempLists(aBuilder);
  310. nsBoxFrame::BuildDisplayListForChildren(aBuilder, tempLists);
  311. // This is a bit of a hack. Collect up all descendant display items
  312. // and merge them into a single Content() list.
  313. nsDisplayList masterList;
  314. masterList.AppendToTop(tempLists.BorderBackground());
  315. masterList.AppendToTop(tempLists.BlockBorderBackgrounds());
  316. masterList.AppendToTop(tempLists.Floats());
  317. masterList.AppendToTop(tempLists.Content());
  318. masterList.AppendToTop(tempLists.PositionedDescendants());
  319. masterList.AppendToTop(tempLists.Outlines());
  320. // Wrap the list to make it its own layer.
  321. aLists.Content()->AppendNewToTop(new (aBuilder)
  322. nsDisplayOwnLayer(aBuilder, this, &masterList, flags, scrollTargetId,
  323. GetThumbRatio()));
  324. return;
  325. }
  326. }
  327. nsBoxFrame::BuildDisplayListForChildren(aBuilder, aLists);
  328. }
  329. NS_IMETHODIMP
  330. nsSliderFrame::DoXULLayout(nsBoxLayoutState& aState)
  331. {
  332. // get the thumb should be our only child
  333. nsIFrame* thumbBox = nsBox::GetChildXULBox(this);
  334. if (!thumbBox) {
  335. SyncLayout(aState);
  336. return NS_OK;
  337. }
  338. EnsureOrient();
  339. #ifdef DEBUG_LAYOUT
  340. if (mState & NS_STATE_DEBUG_WAS_SET) {
  341. if (mState & NS_STATE_SET_TO_DEBUG)
  342. SetXULDebug(aState, true);
  343. else
  344. SetXULDebug(aState, false);
  345. }
  346. #endif
  347. // get the content area inside our borders
  348. nsRect clientRect;
  349. GetXULClientRect(clientRect);
  350. // get the scrollbar
  351. nsIFrame* scrollbarBox = GetScrollbar();
  352. nsCOMPtr<nsIContent> scrollbar;
  353. scrollbar = GetContentOfBox(scrollbarBox);
  354. // get the thumb's pref size
  355. nsSize thumbSize = thumbBox->GetXULPrefSize(aState);
  356. if (IsXULHorizontal())
  357. thumbSize.height = clientRect.height;
  358. else
  359. thumbSize.width = clientRect.width;
  360. int32_t curPos = GetCurrentPosition(scrollbar);
  361. int32_t minPos = GetMinPosition(scrollbar);
  362. int32_t maxPos = GetMaxPosition(scrollbar);
  363. int32_t pageIncrement = GetPageIncrement(scrollbar);
  364. maxPos = std::max(minPos, maxPos);
  365. curPos = clamped(curPos, minPos, maxPos);
  366. nscoord& availableLength = IsXULHorizontal() ? clientRect.width : clientRect.height;
  367. nscoord& thumbLength = IsXULHorizontal() ? thumbSize.width : thumbSize.height;
  368. if ((pageIncrement + maxPos - minPos) > 0 && thumbBox->GetXULFlex() > 0) {
  369. float ratio = float(pageIncrement) / float(maxPos - minPos + pageIncrement);
  370. thumbLength = std::max(thumbLength, NSToCoordRound(availableLength * ratio));
  371. }
  372. // Round the thumb's length to device pixels.
  373. nsPresContext* presContext = PresContext();
  374. thumbLength = presContext->DevPixelsToAppUnits(
  375. presContext->AppUnitsToDevPixels(thumbLength));
  376. // mRatio translates the thumb position in app units to the value.
  377. mRatio = (minPos != maxPos) ? float(availableLength - thumbLength) / float(maxPos - minPos) : 1;
  378. // in reverse mode, curpos is reversed such that lower values are to the
  379. // right or bottom and increase leftwards or upwards. In this case, use the
  380. // offset from the end instead of the beginning.
  381. bool reverse = mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::dir,
  382. nsGkAtoms::reverse, eCaseMatters);
  383. nscoord pos = reverse ? (maxPos - curPos) : (curPos - minPos);
  384. // set the thumb's coord to be the current pos * the ratio.
  385. nsRect thumbRect(clientRect.x, clientRect.y, thumbSize.width, thumbSize.height);
  386. int32_t& thumbPos = (IsXULHorizontal() ? thumbRect.x : thumbRect.y);
  387. thumbPos += NSToCoordRound(pos * mRatio);
  388. nsRect oldThumbRect(thumbBox->GetRect());
  389. LayoutChildAt(aState, thumbBox, thumbRect);
  390. SyncLayout(aState);
  391. // Redraw only if thumb changed size.
  392. if (!oldThumbRect.IsEqualInterior(thumbRect))
  393. XULRedraw(aState);
  394. return NS_OK;
  395. }
  396. nsresult
  397. nsSliderFrame::HandleEvent(nsPresContext* aPresContext,
  398. WidgetGUIEvent* aEvent,
  399. nsEventStatus* aEventStatus)
  400. {
  401. NS_ENSURE_ARG_POINTER(aEventStatus);
  402. // If a web page calls event.preventDefault() we still want to
  403. // scroll when scroll arrow is clicked. See bug 511075.
  404. if (!mContent->IsInNativeAnonymousSubtree() &&
  405. nsEventStatus_eConsumeNoDefault == *aEventStatus) {
  406. return NS_OK;
  407. }
  408. if (!mDragFinished && !isDraggingThumb()) {
  409. StopDrag();
  410. return NS_OK;
  411. }
  412. nsIFrame* scrollbarBox = GetScrollbar();
  413. nsCOMPtr<nsIContent> scrollbar;
  414. scrollbar = GetContentOfBox(scrollbarBox);
  415. bool isHorizontal = IsXULHorizontal();
  416. if (isDraggingThumb())
  417. {
  418. switch (aEvent->mMessage) {
  419. case eTouchMove:
  420. case eMouseMove: {
  421. if (mScrollingWithAPZ) {
  422. break;
  423. }
  424. nsPoint eventPoint;
  425. if (!GetEventPoint(aEvent, eventPoint)) {
  426. break;
  427. }
  428. if (mChange) {
  429. // On Linux the destination point is determined by the initial click
  430. // on the scrollbar track and doesn't change until the mouse button
  431. // is released.
  432. #ifndef MOZ_WIDGET_GTK
  433. // On the other platforms we need to update the destination point now.
  434. mDestinationPoint = eventPoint;
  435. StopRepeat();
  436. StartRepeat();
  437. #endif
  438. break;
  439. }
  440. nscoord pos = isHorizontal ? eventPoint.x : eventPoint.y;
  441. nsIFrame* thumbFrame = mFrames.FirstChild();
  442. if (!thumbFrame) {
  443. return NS_OK;
  444. }
  445. // take our current position and subtract the start location
  446. pos -= mDragStart;
  447. bool isMouseOutsideThumb = false;
  448. if (gSnapMultiplier) {
  449. nsSize thumbSize = thumbFrame->GetSize();
  450. if (isHorizontal) {
  451. // horizontal scrollbar - check if mouse is above or below thumb
  452. // XXXbz what about looking at the .y of the thumb's rect? Is that
  453. // always zero here?
  454. if (eventPoint.y < -gSnapMultiplier * thumbSize.height ||
  455. eventPoint.y > thumbSize.height +
  456. gSnapMultiplier * thumbSize.height)
  457. isMouseOutsideThumb = true;
  458. }
  459. else {
  460. // vertical scrollbar - check if mouse is left or right of thumb
  461. if (eventPoint.x < -gSnapMultiplier * thumbSize.width ||
  462. eventPoint.x > thumbSize.width +
  463. gSnapMultiplier * thumbSize.width)
  464. isMouseOutsideThumb = true;
  465. }
  466. }
  467. if (aEvent->mClass == eTouchEventClass) {
  468. *aEventStatus = nsEventStatus_eConsumeNoDefault;
  469. }
  470. if (isMouseOutsideThumb)
  471. {
  472. SetCurrentThumbPosition(scrollbar, mThumbStart, false, false);
  473. return NS_OK;
  474. }
  475. // set it
  476. SetCurrentThumbPosition(scrollbar, pos, false, true); // with snapping
  477. }
  478. break;
  479. case eTouchEnd:
  480. case eMouseUp:
  481. if (ShouldScrollForEvent(aEvent)) {
  482. StopDrag();
  483. //we MUST call nsFrame HandleEvent for mouse ups to maintain the selection state and capture state.
  484. return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
  485. }
  486. break;
  487. default:
  488. break;
  489. }
  490. //return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
  491. return NS_OK;
  492. } else if (ShouldScrollToClickForEvent(aEvent)) {
  493. nsPoint eventPoint;
  494. if (!GetEventPoint(aEvent, eventPoint)) {
  495. return NS_OK;
  496. }
  497. nscoord pos = isHorizontal ? eventPoint.x : eventPoint.y;
  498. // adjust so that the middle of the thumb is placed under the click
  499. nsIFrame* thumbFrame = mFrames.FirstChild();
  500. if (!thumbFrame) {
  501. return NS_OK;
  502. }
  503. nsSize thumbSize = thumbFrame->GetSize();
  504. nscoord thumbLength = isHorizontal ? thumbSize.width : thumbSize.height;
  505. // set it
  506. nsWeakFrame weakFrame(this);
  507. // should aMaySnap be true here?
  508. SetCurrentThumbPosition(scrollbar, pos - thumbLength/2, false, false);
  509. NS_ENSURE_TRUE(weakFrame.IsAlive(), NS_OK);
  510. DragThumb(true);
  511. #ifdef MOZ_WIDGET_GTK
  512. nsCOMPtr<nsIContent> thumb = thumbFrame->GetContent();
  513. thumb->SetAttr(kNameSpaceID_None, nsGkAtoms::active, NS_LITERAL_STRING("true"), true);
  514. #endif
  515. if (aEvent->mClass == eTouchEventClass) {
  516. *aEventStatus = nsEventStatus_eConsumeNoDefault;
  517. }
  518. if (isHorizontal)
  519. mThumbStart = thumbFrame->GetPosition().x;
  520. else
  521. mThumbStart = thumbFrame->GetPosition().y;
  522. mDragStart = pos - mThumbStart;
  523. }
  524. #ifdef MOZ_WIDGET_GTK
  525. else if (ShouldScrollForEvent(aEvent) &&
  526. aEvent->mClass == eMouseEventClass &&
  527. aEvent->AsMouseEvent()->button == WidgetMouseEvent::eRightButton) {
  528. // HandlePress and HandleRelease are usually called via
  529. // nsFrame::HandleEvent, but only for the left mouse button.
  530. if (aEvent->mMessage == eMouseDown) {
  531. HandlePress(aPresContext, aEvent, aEventStatus);
  532. } else if (aEvent->mMessage == eMouseUp) {
  533. HandleRelease(aPresContext, aEvent, aEventStatus);
  534. }
  535. return NS_OK;
  536. }
  537. #endif
  538. // XXX hack until handle release is actually called in nsframe.
  539. // if (aEvent->mMessage == eMouseOut ||
  540. // aEvent->mMessage == NS_MOUSE_RIGHT_BUTTON_UP ||
  541. // aEvent->mMessage == NS_MOUSE_LEFT_BUTTON_UP) {
  542. // HandleRelease(aPresContext, aEvent, aEventStatus);
  543. // }
  544. if (aEvent->mMessage == eMouseOut && mChange)
  545. HandleRelease(aPresContext, aEvent, aEventStatus);
  546. return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
  547. }
  548. // Helper function to collect the "scroll to click" metric. Beware of
  549. // caching this, users expect to be able to change the system preference
  550. // and see the browser change its behavior immediately.
  551. bool
  552. nsSliderFrame::GetScrollToClick()
  553. {
  554. if (GetScrollbar() != this) {
  555. return LookAndFeel::GetInt(LookAndFeel::eIntID_ScrollToClick, false);
  556. }
  557. if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::movetoclick,
  558. nsGkAtoms::_true, eCaseMatters)) {
  559. return true;
  560. }
  561. if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::movetoclick,
  562. nsGkAtoms::_false, eCaseMatters)) {
  563. return false;
  564. }
  565. return false;
  566. }
  567. nsIFrame*
  568. nsSliderFrame::GetScrollbar()
  569. {
  570. // if we are in a scrollbar then return the scrollbar's content node
  571. // if we are not then return ours.
  572. nsIFrame* scrollbar;
  573. nsScrollbarButtonFrame::GetParentWithTag(nsGkAtoms::scrollbar, this, scrollbar);
  574. if (scrollbar == nullptr)
  575. return this;
  576. return scrollbar->IsXULBoxFrame() ? scrollbar : this;
  577. }
  578. void
  579. nsSliderFrame::PageUpDown(nscoord change)
  580. {
  581. // on a page up or down get our page increment. We get this by getting the scrollbar we are in and
  582. // asking it for the current position and the page increment. If we are not in a scrollbar we will
  583. // get the values from our own node.
  584. nsIFrame* scrollbarBox = GetScrollbar();
  585. nsCOMPtr<nsIContent> scrollbar;
  586. scrollbar = GetContentOfBox(scrollbarBox);
  587. nscoord pageIncrement = GetPageIncrement(scrollbar);
  588. int32_t curpos = GetCurrentPosition(scrollbar);
  589. int32_t minpos = GetMinPosition(scrollbar);
  590. int32_t maxpos = GetMaxPosition(scrollbar);
  591. // get the new position and make sure it is in bounds
  592. int32_t newpos = curpos + change * pageIncrement;
  593. if (newpos < minpos || maxpos < minpos)
  594. newpos = minpos;
  595. else if (newpos > maxpos)
  596. newpos = maxpos;
  597. SetCurrentPositionInternal(scrollbar, newpos, true);
  598. }
  599. // called when the current position changed and we need to update the thumb's location
  600. void
  601. nsSliderFrame::CurrentPositionChanged()
  602. {
  603. nsIFrame* scrollbarBox = GetScrollbar();
  604. nsCOMPtr<nsIContent> scrollbar;
  605. scrollbar = GetContentOfBox(scrollbarBox);
  606. // get the current position
  607. int32_t curPos = GetCurrentPosition(scrollbar);
  608. // do nothing if the position did not change
  609. if (mCurPos == curPos)
  610. return;
  611. // get our current min and max position from our content node
  612. int32_t minPos = GetMinPosition(scrollbar);
  613. int32_t maxPos = GetMaxPosition(scrollbar);
  614. maxPos = std::max(minPos, maxPos);
  615. curPos = clamped(curPos, minPos, maxPos);
  616. // get the thumb's rect
  617. nsIFrame* thumbFrame = mFrames.FirstChild();
  618. if (!thumbFrame)
  619. return; // The thumb may stream in asynchronously via XBL.
  620. nsRect thumbRect = thumbFrame->GetRect();
  621. nsRect clientRect;
  622. GetXULClientRect(clientRect);
  623. // figure out the new rect
  624. nsRect newThumbRect(thumbRect);
  625. bool reverse = mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::dir,
  626. nsGkAtoms::reverse, eCaseMatters);
  627. nscoord pos = reverse ? (maxPos - curPos) : (curPos - minPos);
  628. if (IsXULHorizontal())
  629. newThumbRect.x = clientRect.x + NSToCoordRound(pos * mRatio);
  630. else
  631. newThumbRect.y = clientRect.y + NSToCoordRound(pos * mRatio);
  632. // avoid putting the scroll thumb at subpixel positions which cause needless invalidations
  633. nscoord appUnitsPerPixel = PresContext()->AppUnitsPerDevPixel();
  634. nsPoint snappedThumbLocation = ToAppUnits(
  635. newThumbRect.TopLeft().ToNearestPixels(appUnitsPerPixel),
  636. appUnitsPerPixel);
  637. if (IsXULHorizontal()) {
  638. newThumbRect.x = snappedThumbLocation.x;
  639. } else {
  640. newThumbRect.y = snappedThumbLocation.y;
  641. }
  642. // set the rect
  643. thumbFrame->SetRect(newThumbRect);
  644. // Request a repaint of the scrollbar
  645. nsScrollbarFrame* scrollbarFrame = do_QueryFrame(scrollbarBox);
  646. nsIScrollbarMediator* mediator = scrollbarFrame
  647. ? scrollbarFrame->GetScrollbarMediator() : nullptr;
  648. if (!mediator || !mediator->ShouldSuppressScrollbarRepaints()) {
  649. SchedulePaint();
  650. }
  651. mCurPos = curPos;
  652. // inform the parent <scale> if it exists that the value changed
  653. nsIFrame* parent = GetParent();
  654. if (parent) {
  655. nsCOMPtr<nsISliderListener> sliderListener = do_QueryInterface(parent->GetContent());
  656. if (sliderListener) {
  657. nsContentUtils::AddScriptRunner(
  658. new nsValueChangedRunnable(sliderListener, nsGkAtoms::curpos, mCurPos, mUserChanged));
  659. }
  660. }
  661. }
  662. static void UpdateAttribute(nsIContent* aScrollbar, nscoord aNewPos, bool aNotify, bool aIsSmooth) {
  663. nsAutoString str;
  664. str.AppendInt(aNewPos);
  665. if (aIsSmooth) {
  666. aScrollbar->SetAttr(kNameSpaceID_None, nsGkAtoms::smooth, NS_LITERAL_STRING("true"), false);
  667. }
  668. aScrollbar->SetAttr(kNameSpaceID_None, nsGkAtoms::curpos, str, aNotify);
  669. if (aIsSmooth) {
  670. aScrollbar->UnsetAttr(kNameSpaceID_None, nsGkAtoms::smooth, false);
  671. }
  672. }
  673. // Use this function when you want to set the scroll position via the position
  674. // of the scrollbar thumb, e.g. when dragging the slider. This function scrolls
  675. // the content in such a way that thumbRect.x/.y becomes aNewThumbPos.
  676. void
  677. nsSliderFrame::SetCurrentThumbPosition(nsIContent* aScrollbar, nscoord aNewThumbPos,
  678. bool aIsSmooth, bool aMaySnap)
  679. {
  680. nsRect crect;
  681. GetXULClientRect(crect);
  682. nscoord offset = IsXULHorizontal() ? crect.x : crect.y;
  683. int32_t newPos = NSToIntRound((aNewThumbPos - offset) / mRatio);
  684. if (aMaySnap && mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::snap,
  685. nsGkAtoms::_true, eCaseMatters)) {
  686. // If snap="true", then the slider may only be set to min + (increment * x).
  687. // Otherwise, the slider may be set to any positive integer.
  688. int32_t increment = GetIncrement(aScrollbar);
  689. newPos = NSToIntRound(newPos / float(increment)) * increment;
  690. }
  691. SetCurrentPosition(aScrollbar, newPos, aIsSmooth);
  692. }
  693. // Use this function when you know the target scroll position of the scrolled content.
  694. // aNewPos should be passed to this function as a position as if the minpos is 0.
  695. // That is, the minpos will be added to the position by this function. In a reverse
  696. // direction slider, the newpos should be the distance from the end.
  697. void
  698. nsSliderFrame::SetCurrentPosition(nsIContent* aScrollbar, int32_t aNewPos,
  699. bool aIsSmooth)
  700. {
  701. // get min and max position from our content node
  702. int32_t minpos = GetMinPosition(aScrollbar);
  703. int32_t maxpos = GetMaxPosition(aScrollbar);
  704. // in reverse direction sliders, flip the value so that it goes from
  705. // right to left, or bottom to top.
  706. if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::dir,
  707. nsGkAtoms::reverse, eCaseMatters))
  708. aNewPos = maxpos - aNewPos;
  709. else
  710. aNewPos += minpos;
  711. // get the new position and make sure it is in bounds
  712. if (aNewPos < minpos || maxpos < minpos)
  713. aNewPos = minpos;
  714. else if (aNewPos > maxpos)
  715. aNewPos = maxpos;
  716. SetCurrentPositionInternal(aScrollbar, aNewPos, aIsSmooth);
  717. }
  718. void
  719. nsSliderFrame::SetCurrentPositionInternal(nsIContent* aScrollbar, int32_t aNewPos,
  720. bool aIsSmooth)
  721. {
  722. nsCOMPtr<nsIContent> scrollbar = aScrollbar;
  723. nsIFrame* scrollbarBox = GetScrollbar();
  724. nsWeakFrame weakFrame(this);
  725. mUserChanged = true;
  726. nsScrollbarFrame* scrollbarFrame = do_QueryFrame(scrollbarBox);
  727. if (scrollbarFrame) {
  728. // See if we have a mediator.
  729. nsIScrollbarMediator* mediator = scrollbarFrame->GetScrollbarMediator();
  730. if (mediator) {
  731. nscoord oldPos = nsPresContext::CSSPixelsToAppUnits(GetCurrentPosition(scrollbar));
  732. nscoord newPos = nsPresContext::CSSPixelsToAppUnits(aNewPos);
  733. mediator->ThumbMoved(scrollbarFrame, oldPos, newPos);
  734. if (!weakFrame.IsAlive()) {
  735. return;
  736. }
  737. UpdateAttribute(scrollbar, aNewPos, /* aNotify */false, aIsSmooth);
  738. CurrentPositionChanged();
  739. mUserChanged = false;
  740. return;
  741. }
  742. }
  743. UpdateAttribute(scrollbar, aNewPos, true, aIsSmooth);
  744. if (!weakFrame.IsAlive()) {
  745. return;
  746. }
  747. mUserChanged = false;
  748. #ifdef DEBUG_SLIDER
  749. printf("Current Pos=%d\n",aNewPos);
  750. #endif
  751. }
  752. nsIAtom*
  753. nsSliderFrame::GetType() const
  754. {
  755. return nsGkAtoms::sliderFrame;
  756. }
  757. void
  758. nsSliderFrame::SetInitialChildList(ChildListID aListID,
  759. nsFrameList& aChildList)
  760. {
  761. nsBoxFrame::SetInitialChildList(aListID, aChildList);
  762. if (aListID == kPrincipalList) {
  763. AddListener();
  764. }
  765. }
  766. nsresult
  767. nsSliderMediator::HandleEvent(nsIDOMEvent* aEvent)
  768. {
  769. // Only process the event if the thumb is not being dragged.
  770. if (mSlider && !mSlider->isDraggingThumb())
  771. return mSlider->StartDrag(aEvent);
  772. return NS_OK;
  773. }
  774. bool
  775. nsSliderFrame::StartAPZDrag()
  776. {
  777. if (!gfxPlatform::GetPlatform()->SupportsApzDragInput()) {
  778. return false;
  779. }
  780. nsContainerFrame* cf = GetScrollbar()->GetParent();
  781. if (!cf) {
  782. return false;
  783. }
  784. nsIContent* scrollableContent = cf->GetContent();
  785. if (!scrollableContent) {
  786. return false;
  787. }
  788. mozilla::layers::FrameMetrics::ViewID scrollTargetId;
  789. bool hasID = nsLayoutUtils::FindIDFor(scrollableContent, &scrollTargetId);
  790. bool hasAPZView = hasID && (scrollTargetId != layers::FrameMetrics::NULL_SCROLL_ID);
  791. if (!hasAPZView) {
  792. return false;
  793. }
  794. nsIFrame* scrollbarBox = GetScrollbar();
  795. nsCOMPtr<nsIContent> scrollbar = GetContentOfBox(scrollbarBox);
  796. // This rect is the range in which the scroll thumb can slide in.
  797. nsRect sliderTrack = GetRect() - scrollbarBox->GetPosition();
  798. CSSIntRect sliderTrackCSS = CSSIntRect::FromAppUnitsRounded(sliderTrack);
  799. uint64_t inputblockId = InputAPZContext::GetInputBlockId();
  800. uint32_t presShellId = PresContext()->PresShell()->GetPresShellId();
  801. AsyncDragMetrics dragMetrics(scrollTargetId, presShellId, inputblockId,
  802. NSAppUnitsToIntPixels(mDragStart,
  803. float(AppUnitsPerCSSPixel())),
  804. sliderTrackCSS,
  805. IsXULHorizontal() ? AsyncDragMetrics::HORIZONTAL :
  806. AsyncDragMetrics::VERTICAL);
  807. if (!nsLayoutUtils::HasDisplayPort(scrollableContent)) {
  808. return false;
  809. }
  810. // When we start an APZ drag, we wont get mouse events for the drag.
  811. // APZ will consume them all and only notify us of the new scroll position.
  812. this->GetNearestWidget()->StartAsyncScrollbarDrag(dragMetrics);
  813. return true;
  814. }
  815. nsresult
  816. nsSliderFrame::StartDrag(nsIDOMEvent* aEvent)
  817. {
  818. #ifdef DEBUG_SLIDER
  819. printf("Begin dragging\n");
  820. #endif
  821. if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
  822. nsGkAtoms::_true, eCaseMatters))
  823. return NS_OK;
  824. WidgetGUIEvent* event = aEvent->WidgetEventPtr()->AsGUIEvent();
  825. if (!ShouldScrollForEvent(event)) {
  826. return NS_OK;
  827. }
  828. nsPoint pt;
  829. if (!GetEventPoint(event, pt)) {
  830. return NS_OK;
  831. }
  832. bool isHorizontal = IsXULHorizontal();
  833. nscoord pos = isHorizontal ? pt.x : pt.y;
  834. // If we should scroll-to-click, first place the middle of the slider thumb
  835. // under the mouse.
  836. nsCOMPtr<nsIContent> scrollbar;
  837. nscoord newpos = pos;
  838. bool scrollToClick = ShouldScrollToClickForEvent(event);
  839. if (scrollToClick) {
  840. // adjust so that the middle of the thumb is placed under the click
  841. nsIFrame* thumbFrame = mFrames.FirstChild();
  842. if (!thumbFrame) {
  843. return NS_OK;
  844. }
  845. nsSize thumbSize = thumbFrame->GetSize();
  846. nscoord thumbLength = isHorizontal ? thumbSize.width : thumbSize.height;
  847. newpos -= (thumbLength/2);
  848. nsIFrame* scrollbarBox = GetScrollbar();
  849. scrollbar = GetContentOfBox(scrollbarBox);
  850. }
  851. DragThumb(true);
  852. if (scrollToClick) {
  853. // should aMaySnap be true here?
  854. SetCurrentThumbPosition(scrollbar, newpos, false, false);
  855. }
  856. nsIFrame* thumbFrame = mFrames.FirstChild();
  857. if (!thumbFrame) {
  858. return NS_OK;
  859. }
  860. #ifdef MOZ_WIDGET_GTK
  861. nsCOMPtr<nsIContent> thumb = thumbFrame->GetContent();
  862. thumb->SetAttr(kNameSpaceID_None, nsGkAtoms::active, NS_LITERAL_STRING("true"), true);
  863. #endif
  864. if (isHorizontal)
  865. mThumbStart = thumbFrame->GetPosition().x;
  866. else
  867. mThumbStart = thumbFrame->GetPosition().y;
  868. mDragStart = pos - mThumbStart;
  869. mScrollingWithAPZ = StartAPZDrag();
  870. #ifdef DEBUG_SLIDER
  871. printf("Pressed mDragStart=%d\n",mDragStart);
  872. #endif
  873. if (!mScrollingWithAPZ && !mSuppressionActive) {
  874. MOZ_ASSERT(PresContext()->PresShell());
  875. APZCCallbackHelper::SuppressDisplayport(true, PresContext()->PresShell());
  876. mSuppressionActive = true;
  877. }
  878. return NS_OK;
  879. }
  880. nsresult
  881. nsSliderFrame::StopDrag()
  882. {
  883. AddListener();
  884. DragThumb(false);
  885. mScrollingWithAPZ = false;
  886. if (mSuppressionActive) {
  887. MOZ_ASSERT(PresContext()->PresShell());
  888. APZCCallbackHelper::SuppressDisplayport(false, PresContext()->PresShell());
  889. mSuppressionActive = false;
  890. }
  891. #ifdef MOZ_WIDGET_GTK
  892. nsIFrame* thumbFrame = mFrames.FirstChild();
  893. if (thumbFrame) {
  894. nsCOMPtr<nsIContent> thumb = thumbFrame->GetContent();
  895. thumb->UnsetAttr(kNameSpaceID_None, nsGkAtoms::active, true);
  896. }
  897. #endif
  898. if (mChange) {
  899. StopRepeat();
  900. mChange = 0;
  901. }
  902. return NS_OK;
  903. }
  904. void
  905. nsSliderFrame::DragThumb(bool aGrabMouseEvents)
  906. {
  907. mDragFinished = !aGrabMouseEvents;
  908. // inform the parent <scale> that a drag is beginning or ending
  909. nsIFrame* parent = GetParent();
  910. if (parent) {
  911. nsCOMPtr<nsISliderListener> sliderListener = do_QueryInterface(parent->GetContent());
  912. if (sliderListener) {
  913. nsContentUtils::AddScriptRunner(
  914. new nsDragStateChangedRunnable(sliderListener, aGrabMouseEvents));
  915. }
  916. }
  917. nsIPresShell::SetCapturingContent(aGrabMouseEvents ? GetContent() : nullptr,
  918. aGrabMouseEvents ? CAPTURE_IGNOREALLOWED : 0);
  919. }
  920. bool
  921. nsSliderFrame::isDraggingThumb()
  922. {
  923. return (nsIPresShell::GetCapturingContent() == GetContent());
  924. }
  925. void
  926. nsSliderFrame::AddListener()
  927. {
  928. if (!mMediator) {
  929. mMediator = new nsSliderMediator(this);
  930. }
  931. nsIFrame* thumbFrame = mFrames.FirstChild();
  932. if (!thumbFrame) {
  933. return;
  934. }
  935. thumbFrame->GetContent()->
  936. AddSystemEventListener(NS_LITERAL_STRING("mousedown"), mMediator,
  937. false, false);
  938. thumbFrame->GetContent()->
  939. AddSystemEventListener(NS_LITERAL_STRING("touchstart"), mMediator,
  940. false, false);
  941. }
  942. void
  943. nsSliderFrame::RemoveListener()
  944. {
  945. NS_ASSERTION(mMediator, "No listener was ever added!!");
  946. nsIFrame* thumbFrame = mFrames.FirstChild();
  947. if (!thumbFrame)
  948. return;
  949. thumbFrame->GetContent()->
  950. RemoveSystemEventListener(NS_LITERAL_STRING("mousedown"), mMediator, false);
  951. }
  952. bool
  953. nsSliderFrame::ShouldScrollForEvent(WidgetGUIEvent* aEvent)
  954. {
  955. switch (aEvent->mMessage) {
  956. case eTouchStart:
  957. case eTouchEnd:
  958. return true;
  959. case eMouseDown:
  960. case eMouseUp: {
  961. uint16_t button = aEvent->AsMouseEvent()->button;
  962. #ifdef MOZ_WIDGET_GTK
  963. return (button == WidgetMouseEvent::eLeftButton) ||
  964. (button == WidgetMouseEvent::eRightButton && GetScrollToClick()) ||
  965. (button == WidgetMouseEvent::eMiddleButton && gMiddlePref && !GetScrollToClick());
  966. #else
  967. return (button == WidgetMouseEvent::eLeftButton) ||
  968. (button == WidgetMouseEvent::eMiddleButton && gMiddlePref);
  969. #endif
  970. }
  971. default:
  972. return false;
  973. }
  974. }
  975. bool
  976. nsSliderFrame::ShouldScrollToClickForEvent(WidgetGUIEvent* aEvent)
  977. {
  978. if (!ShouldScrollForEvent(aEvent)) {
  979. return false;
  980. }
  981. if (aEvent->mMessage == eTouchStart) {
  982. return GetScrollToClick();
  983. }
  984. if (aEvent->mMessage != eMouseDown) {
  985. return false;
  986. }
  987. #if defined(MOZ_WIDGET_GTK)
  988. // On Linux, clicking the scrollbar thumb should never scroll to click.
  989. if (IsEventOverThumb(aEvent)) {
  990. return false;
  991. }
  992. #endif
  993. WidgetMouseEvent* mouseEvent = aEvent->AsMouseEvent();
  994. if (mouseEvent->button == WidgetMouseEvent::eLeftButton) {
  995. bool invertPref = mouseEvent->IsShift();
  996. return GetScrollToClick() != invertPref;
  997. }
  998. #ifdef MOZ_WIDGET_GTK
  999. if (mouseEvent->button == WidgetMouseEvent::eRightButton) {
  1000. return !GetScrollToClick();
  1001. }
  1002. #endif
  1003. return true;
  1004. }
  1005. bool
  1006. nsSliderFrame::IsEventOverThumb(WidgetGUIEvent* aEvent)
  1007. {
  1008. nsIFrame* thumbFrame = mFrames.FirstChild();
  1009. if (!thumbFrame) {
  1010. return false;
  1011. }
  1012. nsPoint eventPoint;
  1013. if (!GetEventPoint(aEvent, eventPoint)) {
  1014. return false;
  1015. }
  1016. nsRect thumbRect = thumbFrame->GetRect();
  1017. #if defined(MOZ_WIDGET_GTK)
  1018. /* Scrollbar track can have padding, so it's better to check that eventPoint
  1019. * is inside of actual thumb, not just its one axis. The part of the scrollbar
  1020. * track adjacent to thumb can actually receive events in GTK3 */
  1021. return eventPoint.x >= thumbRect.x && eventPoint.x < thumbRect.XMost() &&
  1022. eventPoint.y >= thumbRect.y && eventPoint.y < thumbRect.YMost();
  1023. #else
  1024. bool isHorizontal = IsXULHorizontal();
  1025. nscoord eventPos = isHorizontal ? eventPoint.x : eventPoint.y;
  1026. nscoord thumbStart = isHorizontal ? thumbRect.x : thumbRect.y;
  1027. nscoord thumbEnd = isHorizontal ? thumbRect.XMost() : thumbRect.YMost();
  1028. return eventPos >= thumbStart && eventPos < thumbEnd;
  1029. #endif
  1030. }
  1031. NS_IMETHODIMP
  1032. nsSliderFrame::HandlePress(nsPresContext* aPresContext,
  1033. WidgetGUIEvent* aEvent,
  1034. nsEventStatus* aEventStatus)
  1035. {
  1036. if (!ShouldScrollForEvent(aEvent) || ShouldScrollToClickForEvent(aEvent)) {
  1037. return NS_OK;
  1038. }
  1039. if (IsEventOverThumb(aEvent)) {
  1040. return NS_OK;
  1041. }
  1042. nsIFrame* thumbFrame = mFrames.FirstChild();
  1043. if (!thumbFrame) // display:none?
  1044. return NS_OK;
  1045. if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
  1046. nsGkAtoms::_true, eCaseMatters))
  1047. return NS_OK;
  1048. nsRect thumbRect = thumbFrame->GetRect();
  1049. nscoord change = 1;
  1050. nsPoint eventPoint;
  1051. if (!GetEventPoint(aEvent, eventPoint)) {
  1052. return NS_OK;
  1053. }
  1054. if (IsXULHorizontal() ? eventPoint.x < thumbRect.x
  1055. : eventPoint.y < thumbRect.y)
  1056. change = -1;
  1057. mChange = change;
  1058. DragThumb(true);
  1059. // On Linux we want to keep scrolling in the direction indicated by |change|
  1060. // until the mouse is released. On the other platforms we want to stop
  1061. // scrolling as soon as the scrollbar thumb has reached the current mouse
  1062. // position.
  1063. #ifdef MOZ_WIDGET_GTK
  1064. nsRect clientRect;
  1065. GetXULClientRect(clientRect);
  1066. // Set the destination point to the very end of the scrollbar so that
  1067. // scrolling doesn't stop halfway through.
  1068. if (change > 0) {
  1069. mDestinationPoint = nsPoint(clientRect.width, clientRect.height);
  1070. }
  1071. else {
  1072. mDestinationPoint = nsPoint(0, 0);
  1073. }
  1074. #else
  1075. mDestinationPoint = eventPoint;
  1076. #endif
  1077. StartRepeat();
  1078. PageScroll(change);
  1079. return NS_OK;
  1080. }
  1081. NS_IMETHODIMP
  1082. nsSliderFrame::HandleRelease(nsPresContext* aPresContext,
  1083. WidgetGUIEvent* aEvent,
  1084. nsEventStatus* aEventStatus)
  1085. {
  1086. StopRepeat();
  1087. nsIFrame* scrollbar = GetScrollbar();
  1088. nsScrollbarFrame* sb = do_QueryFrame(scrollbar);
  1089. if (sb) {
  1090. nsIScrollbarMediator* m = sb->GetScrollbarMediator();
  1091. if (m) {
  1092. m->ScrollbarReleased(sb);
  1093. }
  1094. }
  1095. return NS_OK;
  1096. }
  1097. void
  1098. nsSliderFrame::DestroyFrom(nsIFrame* aDestructRoot)
  1099. {
  1100. // tell our mediator if we have one we are gone.
  1101. if (mMediator) {
  1102. mMediator->SetSlider(nullptr);
  1103. mMediator = nullptr;
  1104. }
  1105. StopRepeat();
  1106. // call base class Destroy()
  1107. nsBoxFrame::DestroyFrom(aDestructRoot);
  1108. }
  1109. nsSize
  1110. nsSliderFrame::GetXULPrefSize(nsBoxLayoutState& aState)
  1111. {
  1112. EnsureOrient();
  1113. return nsBoxFrame::GetXULPrefSize(aState);
  1114. }
  1115. nsSize
  1116. nsSliderFrame::GetXULMinSize(nsBoxLayoutState& aState)
  1117. {
  1118. EnsureOrient();
  1119. // our min size is just our borders and padding
  1120. return nsBox::GetXULMinSize(aState);
  1121. }
  1122. nsSize
  1123. nsSliderFrame::GetXULMaxSize(nsBoxLayoutState& aState)
  1124. {
  1125. EnsureOrient();
  1126. return nsBoxFrame::GetXULMaxSize(aState);
  1127. }
  1128. void
  1129. nsSliderFrame::EnsureOrient()
  1130. {
  1131. nsIFrame* scrollbarBox = GetScrollbar();
  1132. bool isHorizontal = (scrollbarBox->GetStateBits() & NS_STATE_IS_HORIZONTAL) != 0;
  1133. if (isHorizontal)
  1134. mState |= NS_STATE_IS_HORIZONTAL;
  1135. else
  1136. mState &= ~NS_STATE_IS_HORIZONTAL;
  1137. }
  1138. void
  1139. nsSliderFrame::Notify(void)
  1140. {
  1141. bool stop = false;
  1142. nsIFrame* thumbFrame = mFrames.FirstChild();
  1143. if (!thumbFrame) {
  1144. StopRepeat();
  1145. return;
  1146. }
  1147. nsRect thumbRect = thumbFrame->GetRect();
  1148. bool isHorizontal = IsXULHorizontal();
  1149. // See if the thumb has moved past our destination point.
  1150. // if it has we want to stop.
  1151. if (isHorizontal) {
  1152. if (mChange < 0) {
  1153. if (thumbRect.x < mDestinationPoint.x)
  1154. stop = true;
  1155. } else {
  1156. if (thumbRect.x + thumbRect.width > mDestinationPoint.x)
  1157. stop = true;
  1158. }
  1159. } else {
  1160. if (mChange < 0) {
  1161. if (thumbRect.y < mDestinationPoint.y)
  1162. stop = true;
  1163. } else {
  1164. if (thumbRect.y + thumbRect.height > mDestinationPoint.y)
  1165. stop = true;
  1166. }
  1167. }
  1168. if (stop) {
  1169. StopRepeat();
  1170. } else {
  1171. PageScroll(mChange);
  1172. }
  1173. }
  1174. void
  1175. nsSliderFrame::PageScroll(nscoord aChange)
  1176. {
  1177. if (mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::dir,
  1178. nsGkAtoms::reverse, eCaseMatters)) {
  1179. aChange = -aChange;
  1180. }
  1181. nsIFrame* scrollbar = GetScrollbar();
  1182. nsScrollbarFrame* sb = do_QueryFrame(scrollbar);
  1183. if (sb) {
  1184. nsIScrollbarMediator* m = sb->GetScrollbarMediator();
  1185. sb->SetIncrementToPage(aChange);
  1186. if (m) {
  1187. m->ScrollByPage(sb, aChange, nsIScrollbarMediator::ENABLE_SNAP);
  1188. return;
  1189. }
  1190. }
  1191. PageUpDown(aChange);
  1192. }
  1193. float
  1194. nsSliderFrame::GetThumbRatio() const
  1195. {
  1196. // mRatio is in thumb app units per scrolled css pixels. Convert it to a
  1197. // ratio of the thumb's CSS pixels per scrolled CSS pixels. (Note the thumb
  1198. // is in the scrollframe's parent's space whereas the scrolled CSS pixels
  1199. // are in the scrollframe's space).
  1200. return mRatio / mozilla::AppUnitsPerCSSPixel();
  1201. }
  1202. NS_IMPL_ISUPPORTS(nsSliderMediator,
  1203. nsIDOMEventListener)