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