/src/mozilla-central/dom/xul/XULScrollElement.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 | | #include "mozilla/dom/Element.h" |
8 | | #include "mozilla/dom/ToJSValue.h" |
9 | | #include "nsCOMPtr.h" |
10 | | #include "nsIPresShell.h" |
11 | | #include "nsIContent.h" |
12 | | #include "nsPresContext.h" |
13 | | #include "nsBox.h" |
14 | | #include "nsIScrollableFrame.h" |
15 | | #include "mozilla/dom/XULScrollElement.h" |
16 | | #include "mozilla/dom/XULScrollElementBinding.h" |
17 | | |
18 | | namespace mozilla { |
19 | | namespace dom { |
20 | | |
21 | | JSObject* |
22 | | XULScrollElement::WrapNode(JSContext *aCx, JS::Handle<JSObject*> aGivenProto) |
23 | 0 | { |
24 | 0 | return XULScrollElement_Binding::Wrap(aCx, this, aGivenProto); |
25 | 0 | } |
26 | | |
27 | | void |
28 | | XULScrollElement::ScrollByIndex(int32_t aIndex, ErrorResult& aRv) |
29 | 0 | { |
30 | 0 | nsIScrollableFrame* sf = GetScrollFrame(); |
31 | 0 | if (!sf) { |
32 | 0 | aRv.Throw(NS_ERROR_FAILURE); |
33 | 0 | return; |
34 | 0 | } |
35 | 0 | |
36 | 0 | nsIFrame* scrolledFrame = sf->GetScrolledFrame(); |
37 | 0 | if (!scrolledFrame){ |
38 | 0 | aRv.Throw(NS_ERROR_FAILURE); |
39 | 0 | return; |
40 | 0 | } |
41 | 0 | |
42 | 0 | nsIFrame* scrolledBox = nsBox::GetChildXULBox(scrolledFrame); |
43 | 0 | if (!scrolledBox){ |
44 | 0 | aRv.Throw(NS_ERROR_FAILURE); |
45 | 0 | return; |
46 | 0 | } |
47 | 0 | |
48 | 0 | nsRect rect; |
49 | 0 |
|
50 | 0 | // now get the element's first child |
51 | 0 | nsIFrame* child = nsBox::GetChildXULBox(scrolledBox); |
52 | 0 |
|
53 | 0 | bool horiz = scrolledBox->IsXULHorizontal(); |
54 | 0 | nsPoint cp = sf->GetScrollPosition(); |
55 | 0 | nscoord diff = 0; |
56 | 0 | int32_t curIndex = 0; |
57 | 0 | bool isLTR = scrolledBox->IsXULNormalDirection(); |
58 | 0 |
|
59 | 0 | nscoord frameWidth = 0; |
60 | 0 | if (!isLTR && horiz) { |
61 | 0 | nsCOMPtr<nsIDocument> doc = GetComposedDoc(); |
62 | 0 |
|
63 | 0 | nsCOMPtr<nsIPresShell> shell = doc->GetShell(); |
64 | 0 | if (!shell) { |
65 | 0 | aRv.Throw(NS_ERROR_UNEXPECTED); |
66 | 0 | return; |
67 | 0 | } |
68 | 0 | nsRect rcFrame = nsLayoutUtils::GetAllInFlowRectsUnion(GetPrimaryFrame(), shell->GetRootFrame()); |
69 | 0 | frameWidth = rcFrame.width; |
70 | 0 | } |
71 | 0 |
|
72 | 0 | // first find out what index we are currently at |
73 | 0 | while(child) { |
74 | 0 | rect = child->GetRect(); |
75 | 0 | if (horiz) { |
76 | 0 | // In the left-to-right case we break from the loop when the center of |
77 | 0 | // the current child rect is greater than the scrolled position of |
78 | 0 | // the left edge of the scrollbox |
79 | 0 | // In the right-to-left case we break when the center of the current |
80 | 0 | // child rect is less than the scrolled position of the right edge of |
81 | 0 | // the scrollelement. |
82 | 0 | diff = rect.x + rect.width/2; // use the center, to avoid rounding errors |
83 | 0 | if ((isLTR && diff > cp.x) || |
84 | 0 | (!isLTR && diff < cp.x + frameWidth)) { |
85 | 0 | break; |
86 | 0 | } |
87 | 0 | } else { |
88 | 0 | diff = rect.y + rect.height/2;// use the center, to avoid rounding errors |
89 | 0 | if (diff > cp.y) { |
90 | 0 | break; |
91 | 0 | } |
92 | 0 | } |
93 | 0 | child = nsBox::GetNextXULBox(child); |
94 | 0 | curIndex++; |
95 | 0 | } |
96 | 0 |
|
97 | 0 | int32_t count = 0; |
98 | 0 |
|
99 | 0 | if (aIndex == 0) |
100 | 0 | return; |
101 | 0 | |
102 | 0 | if (aIndex > 0) { |
103 | 0 | while(child) { |
104 | 0 | child = nsBox::GetNextXULBox(child); |
105 | 0 | if (child) { |
106 | 0 | rect = child->GetRect(); |
107 | 0 | } |
108 | 0 | count++; |
109 | 0 | if (count >= aIndex) { |
110 | 0 | break; |
111 | 0 | } |
112 | 0 | } |
113 | 0 |
|
114 | 0 | } else if (aIndex < 0) { |
115 | 0 | child = nsBox::GetChildXULBox(scrolledBox); |
116 | 0 | while(child) { |
117 | 0 | rect = child->GetRect(); |
118 | 0 | if (count >= curIndex + aIndex) { |
119 | 0 | break; |
120 | 0 | } |
121 | 0 | count++; |
122 | 0 | child = nsBox::GetNextXULBox(child); |
123 | 0 |
|
124 | 0 | } |
125 | 0 | } |
126 | 0 |
|
127 | 0 | nscoord csspixel = nsPresContext::CSSPixelsToAppUnits(1); |
128 | 0 | if (horiz) { |
129 | 0 | // In the left-to-right case we scroll so that the left edge of the |
130 | 0 | // selected child is scrolled to the left edge of the scrollbox. |
131 | 0 | // In the right-to-left case we scroll so that the right edge of the |
132 | 0 | // selected child is scrolled to the right edge of the scrollbox. |
133 | 0 |
|
134 | 0 | nsPoint pt(isLTR ? rect.x : rect.x + rect.width - frameWidth, |
135 | 0 | cp.y); |
136 | 0 |
|
137 | 0 | // Use a destination range that ensures the left edge (or right edge, |
138 | 0 | // for RTL) will indeed be visible. Also ensure that the top edge |
139 | 0 | // is visible. |
140 | 0 | nsRect range(pt.x, pt.y, csspixel, 0); |
141 | 0 | if (isLTR) { |
142 | 0 | range.x -= csspixel; |
143 | 0 | } |
144 | 0 | sf->ScrollTo(pt, nsIScrollableFrame::INSTANT, &range); |
145 | 0 | } else { |
146 | 0 | // Use a destination range that ensures the top edge will be visible. |
147 | 0 | nsRect range(cp.x, rect.y - csspixel, 0, csspixel); |
148 | 0 | sf->ScrollTo(nsPoint(cp.x, rect.y), nsIScrollableFrame::INSTANT, &range); |
149 | 0 | } |
150 | 0 | } |
151 | | |
152 | | void |
153 | | XULScrollElement::ScrollToElement(Element& child, ErrorResult& aRv) |
154 | 0 | { |
155 | 0 | nsCOMPtr<nsIDocument> doc = GetComposedDoc(); |
156 | 0 | if (!doc){ |
157 | 0 | aRv.Throw(NS_ERROR_FAILURE); |
158 | 0 | return; |
159 | 0 | } |
160 | 0 | |
161 | 0 | nsCOMPtr<nsIPresShell> shell = doc->GetShell(); |
162 | 0 | if (!shell) { |
163 | 0 | aRv.Throw(NS_ERROR_UNEXPECTED); |
164 | 0 | return; |
165 | 0 | } |
166 | 0 | |
167 | 0 | shell->ScrollContentIntoView(&child, |
168 | 0 | nsIPresShell::ScrollAxis( |
169 | 0 | nsIPresShell::SCROLL_TOP, |
170 | 0 | nsIPresShell::SCROLL_ALWAYS), |
171 | 0 | nsIPresShell::ScrollAxis( |
172 | 0 | nsIPresShell::SCROLL_LEFT, |
173 | 0 | nsIPresShell::SCROLL_ALWAYS), |
174 | 0 | nsIPresShell::SCROLL_FIRST_ANCESTOR_ONLY | |
175 | 0 | nsIPresShell::SCROLL_OVERFLOW_HIDDEN); |
176 | 0 | } |
177 | | |
178 | | |
179 | | void |
180 | | XULScrollElement::EnsureElementIsVisible(Element& aChild, ErrorResult& aRv) |
181 | 0 | { |
182 | 0 | nsCOMPtr<nsIDocument> doc = GetComposedDoc(); |
183 | 0 | if (!doc){ |
184 | 0 | aRv.Throw(NS_ERROR_FAILURE); |
185 | 0 | return; |
186 | 0 | } |
187 | 0 | |
188 | 0 | nsCOMPtr<nsIPresShell> shell = doc->GetShell(); |
189 | 0 | if (!shell) { |
190 | 0 | aRv.Throw(NS_ERROR_UNEXPECTED); |
191 | 0 | return; |
192 | 0 | } |
193 | 0 | |
194 | 0 | shell->ScrollContentIntoView(&aChild, |
195 | 0 | nsIPresShell::ScrollAxis(), |
196 | 0 | nsIPresShell::ScrollAxis(), |
197 | 0 | nsIPresShell::SCROLL_FIRST_ANCESTOR_ONLY | |
198 | 0 | nsIPresShell::SCROLL_OVERFLOW_HIDDEN); |
199 | 0 | } |
200 | | |
201 | | } // namespace dom |
202 | | } // namespace mozilla |