Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef mozilla_WheelHandlingHelper_h_
8 : #define mozilla_WheelHandlingHelper_h_
9 :
10 : #include "mozilla/Attributes.h"
11 : #include "mozilla/EventForwards.h"
12 : #include "nsCoord.h"
13 : #include "nsIFrame.h"
14 : #include "nsPoint.h"
15 :
16 : class nsIScrollableFrame;
17 : class nsITimer;
18 :
19 : namespace mozilla {
20 :
21 : class EventStateManager;
22 :
23 : /**
24 : * DeltaValues stores two delta values which are along X and Y axis. This is
25 : * useful for arguments and results of some methods.
26 : */
27 :
28 : struct DeltaValues
29 : {
30 : DeltaValues()
31 : : deltaX(0.0)
32 : , deltaY(0.0)
33 : {
34 : }
35 :
36 12 : DeltaValues(double aDeltaX, double aDeltaY)
37 12 : : deltaX(aDeltaX)
38 12 : , deltaY(aDeltaY)
39 : {
40 12 : }
41 :
42 : explicit DeltaValues(WidgetWheelEvent* aEvent);
43 :
44 : double deltaX;
45 : double deltaY;
46 : };
47 :
48 : /**
49 : * WheelHandlingUtils provides some static methods which are useful at handling
50 : * wheel events.
51 : */
52 :
53 : class WheelHandlingUtils
54 : {
55 : public:
56 : /**
57 : * Returns true if aFrame is a scrollable frame and it can be scrolled to
58 : * either aDirectionX or aDirectionY along each axis. Or if aFrame is a
59 : * plugin frame (in this case, aDirectionX and aDirectionY are ignored).
60 : * Otherwise, false.
61 : */
62 : static bool CanScrollOn(nsIFrame* aFrame,
63 : double aDirectionX, double aDirectionY);
64 : /**
65 : * Returns true if the scrollable frame can be scrolled to either aDirectionX
66 : * or aDirectionY along each axis. Otherwise, false.
67 : */
68 : static bool CanScrollOn(nsIScrollableFrame* aScrollFrame,
69 : double aDirectionX, double aDirectionY);
70 :
71 : private:
72 : static bool CanScrollInRange(nscoord aMin, nscoord aValue, nscoord aMax,
73 : double aDirection);
74 : };
75 :
76 : /**
77 : * ScrollbarsForWheel manages scrollbars state during wheel operation.
78 : * E.g., on some platforms, scrollbars should show only while user attempts to
79 : * scroll. At that time, scrollbars which may be possible to scroll by
80 : * operation of wheel at the point should show temporarily.
81 : */
82 :
83 : class ScrollbarsForWheel
84 : {
85 : public:
86 : static void PrepareToScrollText(EventStateManager* aESM,
87 : nsIFrame* aTargetFrame,
88 : WidgetWheelEvent* aEvent);
89 : static void SetActiveScrollTarget(nsIScrollableFrame* aScrollTarget);
90 : // Hide all scrollbars (both mActiveOwner's and mActivatedScrollTargets')
91 : static void MayInactivate();
92 : static void Inactivate();
93 : static bool IsActive();
94 : static void OwnWheelTransaction(bool aOwn);
95 :
96 : protected:
97 : static const size_t kNumberOfTargets = 4;
98 : static const DeltaValues directions[kNumberOfTargets];
99 : static AutoWeakFrame sActiveOwner;
100 : static AutoWeakFrame sActivatedScrollTargets[kNumberOfTargets];
101 : static bool sHadWheelStart;
102 : static bool sOwnWheelTransaction;
103 :
104 :
105 : /**
106 : * These two methods are called upon eWheelOperationStart/eWheelOperationEnd
107 : * events to show/hide the right scrollbars.
108 : */
109 : static void TemporarilyActivateAllPossibleScrollTargets(
110 : EventStateManager* aESM,
111 : nsIFrame* aTargetFrame,
112 : WidgetWheelEvent* aEvent);
113 : static void DeactivateAllTemporarilyActivatedScrollTargets();
114 : };
115 :
116 : /**
117 : * WheelTransaction manages a series of wheel events as a transaction.
118 : * While in a transaction, every wheel event should scroll the same scrollable
119 : * element even if a different scrollable element is under the mouse cursor.
120 : *
121 : * Additionally, this class also manages wheel scroll speed acceleration.
122 : */
123 :
124 : class WheelTransaction
125 : {
126 : public:
127 0 : static nsIFrame* GetTargetFrame() { return sTargetFrame; }
128 : static void EndTransaction();
129 : /**
130 : * WillHandleDefaultAction() is called before handling aWheelEvent on
131 : * aTargetFrame.
132 : *
133 : * @return false if the caller cannot continue to handle the default
134 : * action. Otherwise, true.
135 : */
136 : static bool WillHandleDefaultAction(WidgetWheelEvent* aWheelEvent,
137 : AutoWeakFrame& aTargetWeakFrame);
138 0 : static bool WillHandleDefaultAction(WidgetWheelEvent* aWheelEvent,
139 : nsIFrame* aTargetFrame)
140 : {
141 0 : AutoWeakFrame targetWeakFrame(aTargetFrame);
142 0 : return WillHandleDefaultAction(aWheelEvent, targetWeakFrame);
143 : }
144 : static void OnEvent(WidgetEvent* aEvent);
145 : static void Shutdown();
146 0 : static uint32_t GetTimeoutTime()
147 : {
148 0 : return Prefs::sMouseWheelTransactionTimeout;
149 : }
150 :
151 : static void OwnScrollbars(bool aOwn);
152 :
153 : static DeltaValues AccelerateWheelDelta(WidgetWheelEvent* aEvent,
154 : bool aAllowScrollSpeedOverride);
155 28 : static void InitializeStatics()
156 : {
157 28 : Prefs::InitializeStatics();
158 28 : }
159 :
160 : protected:
161 : static void BeginTransaction(nsIFrame* aTargetFrame,
162 : WidgetWheelEvent* aEvent);
163 : // Be careful, UpdateTransaction may fire a DOM event, therefore, the target
164 : // frame might be destroyed in the event handler.
165 : static bool UpdateTransaction(WidgetWheelEvent* aEvent);
166 : static void MayEndTransaction();
167 :
168 : static LayoutDeviceIntPoint GetScreenPoint(WidgetGUIEvent* aEvent);
169 : static void OnFailToScrollTarget();
170 : static void OnTimeout(nsITimer* aTimer, void* aClosure);
171 : static void SetTimeout();
172 0 : static uint32_t GetIgnoreMoveDelayTime()
173 : {
174 0 : return Prefs::sMouseWheelTransactionIgnoreMoveDelay;
175 : }
176 0 : static int32_t GetAccelerationStart()
177 : {
178 0 : return Prefs::sMouseWheelAccelerationStart;
179 : }
180 0 : static int32_t GetAccelerationFactor()
181 : {
182 0 : return Prefs::sMouseWheelAccelerationFactor;
183 : }
184 : static DeltaValues OverrideSystemScrollSpeed(WidgetWheelEvent* aEvent);
185 : static double ComputeAcceleratedWheelDelta(double aDelta, int32_t aFactor);
186 : static bool OutOfTime(uint32_t aBaseTime, uint32_t aThreshold);
187 :
188 : static AutoWeakFrame sTargetFrame;
189 : static uint32_t sTime; // in milliseconds
190 : static uint32_t sMouseMoved; // in milliseconds
191 : static nsITimer* sTimer;
192 : static int32_t sScrollSeriesCounter;
193 : static bool sOwnScrollbars;
194 :
195 : class Prefs
196 : {
197 : public:
198 : static void InitializeStatics();
199 : static int32_t sMouseWheelAccelerationStart;
200 : static int32_t sMouseWheelAccelerationFactor;
201 : static uint32_t sMouseWheelTransactionTimeout;
202 : static uint32_t sMouseWheelTransactionIgnoreMoveDelay;
203 : static bool sTestMouseScroll;
204 : };
205 : };
206 :
207 : } // namespace mozilla
208 :
209 : #endif // mozilla_WheelHandlingHelper_h_
|