1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Communicator client code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Original Author: David W. Hyatt (hyatt@netscape.com)
24 : * Mihai Șucan <mihai.sucan@gmail.com>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either of the GNU General Public License Version 2 or later (the "GPL"),
28 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "nsCOMPtr.h"
41 : #include "nsIScrollBoxObject.h"
42 : #include "nsBoxObject.h"
43 : #include "nsIPresShell.h"
44 : #include "nsIContent.h"
45 : #include "nsIDocument.h"
46 : #include "nsIDOMDocument.h"
47 : #include "nsIDOMElement.h"
48 : #include "nsPresContext.h"
49 : #include "nsIFrame.h"
50 : #include "nsIScrollableFrame.h"
51 :
52 : class nsScrollBoxObject : public nsIScrollBoxObject, public nsBoxObject
53 : {
54 : public:
55 : NS_DECL_ISUPPORTS_INHERITED
56 : NS_DECL_NSISCROLLBOXOBJECT
57 :
58 : nsScrollBoxObject();
59 : virtual ~nsScrollBoxObject();
60 :
61 0 : virtual nsIScrollableFrame* GetScrollFrame() {
62 0 : return do_QueryFrame(GetFrame(false));
63 : }
64 :
65 : /* additional members */
66 : };
67 :
68 : /* Implementation file */
69 :
70 0 : NS_INTERFACE_MAP_BEGIN(nsScrollBoxObject)
71 0 : NS_INTERFACE_MAP_ENTRY(nsIScrollBoxObject)
72 0 : NS_INTERFACE_MAP_END_INHERITING(nsBoxObject)
73 :
74 0 : NS_IMPL_ADDREF_INHERITED(nsScrollBoxObject, nsBoxObject)
75 0 : NS_IMPL_RELEASE_INHERITED(nsScrollBoxObject, nsBoxObject)
76 :
77 0 : nsScrollBoxObject::nsScrollBoxObject()
78 : {
79 : /* member initializers and constructor code */
80 0 : }
81 :
82 0 : nsScrollBoxObject::~nsScrollBoxObject()
83 : {
84 : /* destructor code */
85 0 : }
86 :
87 : /* void scrollTo (in long x, in long y); */
88 0 : NS_IMETHODIMP nsScrollBoxObject::ScrollTo(PRInt32 x, PRInt32 y)
89 : {
90 0 : nsIScrollableFrame* sf = GetScrollFrame();
91 0 : if (!sf)
92 0 : return NS_ERROR_FAILURE;
93 :
94 : sf->ScrollTo(nsPoint(nsPresContext::CSSPixelsToAppUnits(x),
95 : nsPresContext::CSSPixelsToAppUnits(y)),
96 0 : nsIScrollableFrame::INSTANT);
97 0 : return NS_OK;
98 : }
99 :
100 : /* void scrollBy (in long dx, in long dy); */
101 0 : NS_IMETHODIMP nsScrollBoxObject::ScrollBy(PRInt32 dx, PRInt32 dy)
102 : {
103 : PRInt32 x, y;
104 0 : nsresult rv = GetPosition(&x, &y);
105 0 : if (NS_FAILED(rv))
106 0 : return rv;
107 :
108 0 : return ScrollTo(x + dx, y + dy);
109 : }
110 :
111 : /* void scrollByLine (in long dlines); */
112 0 : NS_IMETHODIMP nsScrollBoxObject::ScrollByLine(PRInt32 dlines)
113 : {
114 0 : nsIScrollableFrame* sf = GetScrollFrame();
115 0 : if (!sf)
116 0 : return NS_ERROR_FAILURE;
117 :
118 : sf->ScrollBy(nsIntPoint(0, dlines), nsIScrollableFrame::LINES,
119 0 : nsIScrollableFrame::SMOOTH);
120 0 : return NS_OK;
121 : }
122 :
123 : // XUL <scrollbox> elements have a single box child element.
124 : // Get a pointer to that box.
125 : // Note that now that the <scrollbox> is just a regular box
126 : // with 'overflow:hidden', the boxobject's frame is an nsXULScrollFrame,
127 : // the <scrollbox>'s box frame is the scrollframe's "scrolled frame", and
128 : // the <scrollbox>'s child box is a child of that.
129 0 : static nsIFrame* GetScrolledBox(nsBoxObject* aScrollBox) {
130 0 : nsIFrame* frame = aScrollBox->GetFrame(false);
131 0 : if (!frame)
132 0 : return nsnull;
133 0 : nsIScrollableFrame* scrollFrame = do_QueryFrame(frame);
134 0 : if (!scrollFrame) {
135 0 : NS_WARNING("nsIScrollBoxObject attached to something that's not a scroll frame!");
136 0 : return nsnull;
137 : }
138 0 : nsIFrame* scrolledFrame = scrollFrame->GetScrolledFrame();
139 0 : if (!scrolledFrame)
140 0 : return nsnull;
141 0 : return scrolledFrame->GetChildBox();
142 : }
143 :
144 : /* void scrollByIndex (in long dindexes); */
145 0 : NS_IMETHODIMP nsScrollBoxObject::ScrollByIndex(PRInt32 dindexes)
146 : {
147 0 : nsIScrollableFrame* sf = GetScrollFrame();
148 0 : if (!sf)
149 0 : return NS_ERROR_FAILURE;
150 0 : nsIFrame* scrolledBox = GetScrolledBox(this);
151 0 : if (!scrolledBox)
152 0 : return NS_ERROR_FAILURE;
153 :
154 0 : nsRect rect;
155 :
156 : // now get the scrolled boxes first child.
157 0 : nsIFrame* child = scrolledBox->GetChildBox();
158 :
159 0 : bool horiz = scrolledBox->IsHorizontal();
160 0 : nsPoint cp = sf->GetScrollPosition();
161 0 : nscoord diff = 0;
162 0 : PRInt32 curIndex = 0;
163 0 : bool isLTR = scrolledBox->IsNormalDirection();
164 :
165 0 : PRInt32 frameWidth = 0;
166 0 : if (!isLTR && horiz) {
167 0 : GetWidth(&frameWidth);
168 0 : nsCOMPtr<nsIPresShell> shell = GetPresShell(false);
169 0 : if (!shell) {
170 0 : return NS_ERROR_UNEXPECTED;
171 : }
172 0 : frameWidth = nsPresContext::CSSPixelsToAppUnits(frameWidth);
173 : }
174 :
175 : // first find out what index we are currently at
176 0 : while(child) {
177 0 : rect = child->GetRect();
178 0 : if (horiz) {
179 : // In the left-to-right case we break from the loop when the center of
180 : // the current child rect is greater than the scrolled position of
181 : // the left edge of the scrollbox
182 : // In the right-to-left case we break when the center of the current
183 : // child rect is less than the scrolled position of the right edge of
184 : // the scrollbox.
185 0 : diff = rect.x + rect.width/2; // use the center, to avoid rounding errors
186 0 : if ((isLTR && diff > cp.x) ||
187 0 : (!isLTR && diff < cp.x + frameWidth)) {
188 0 : break;
189 : }
190 : } else {
191 0 : diff = rect.y + rect.height/2;// use the center, to avoid rounding errors
192 0 : if (diff > cp.y) {
193 0 : break;
194 : }
195 : }
196 0 : child = child->GetNextBox();
197 0 : curIndex++;
198 : }
199 :
200 0 : PRInt32 count = 0;
201 :
202 0 : if (dindexes == 0)
203 0 : return NS_OK;
204 :
205 0 : if (dindexes > 0) {
206 0 : while(child) {
207 0 : child = child->GetNextBox();
208 0 : if (child)
209 0 : rect = child->GetRect();
210 0 : count++;
211 0 : if (count >= dindexes)
212 0 : break;
213 : }
214 :
215 0 : } else if (dindexes < 0) {
216 0 : child = scrolledBox->GetChildBox();
217 0 : while(child) {
218 0 : rect = child->GetRect();
219 0 : if (count >= curIndex + dindexes)
220 0 : break;
221 :
222 0 : count++;
223 0 : child = child->GetNextBox();
224 :
225 : }
226 : }
227 :
228 0 : if (horiz)
229 : // In the left-to-right case we scroll so that the left edge of the
230 : // selected child is scrolled to the left edge of the scrollbox.
231 : // In the right-to-left case we scroll so that the right edge of the
232 : // selected child is scrolled to the right edge of the scrollbox.
233 : sf->ScrollTo(nsPoint(isLTR ? rect.x : rect.x + rect.width - frameWidth,
234 : cp.y),
235 0 : nsIScrollableFrame::INSTANT);
236 : else
237 0 : sf->ScrollTo(nsPoint(cp.x, rect.y), nsIScrollableFrame::INSTANT);
238 :
239 0 : return NS_OK;
240 : }
241 :
242 : /* void scrollToLine (in long line); */
243 0 : NS_IMETHODIMP nsScrollBoxObject::ScrollToLine(PRInt32 line)
244 : {
245 0 : nsIScrollableFrame* sf = GetScrollFrame();
246 0 : if (!sf)
247 0 : return NS_ERROR_FAILURE;
248 :
249 0 : nscoord y = sf->GetLineScrollAmount().height * line;
250 0 : sf->ScrollTo(nsPoint(0, y), nsIScrollableFrame::INSTANT);
251 0 : return NS_OK;
252 : }
253 :
254 : /* void scrollToElement (in nsIDOMElement child); */
255 0 : NS_IMETHODIMP nsScrollBoxObject::ScrollToElement(nsIDOMElement *child)
256 : {
257 0 : NS_ENSURE_ARG_POINTER(child);
258 :
259 0 : nsCOMPtr<nsIPresShell> shell = GetPresShell(false);
260 0 : if (!shell) {
261 0 : return NS_ERROR_UNEXPECTED;
262 : }
263 :
264 0 : nsCOMPtr<nsIContent> content = do_QueryInterface(child);
265 0 : shell->ScrollContentIntoView(content,
266 : NS_PRESSHELL_SCROLL_TOP,
267 : NS_PRESSHELL_SCROLL_LEFT,
268 : nsIPresShell::SCROLL_FIRST_ANCESTOR_ONLY |
269 0 : nsIPresShell::SCROLL_OVERFLOW_HIDDEN);
270 0 : return NS_OK;
271 : }
272 :
273 : /* void scrollToIndex (in long index); */
274 0 : NS_IMETHODIMP nsScrollBoxObject::ScrollToIndex(PRInt32 index)
275 : {
276 0 : return NS_ERROR_NOT_IMPLEMENTED;
277 : }
278 :
279 : /* void getPosition (out long x, out long y); */
280 0 : NS_IMETHODIMP nsScrollBoxObject::GetPosition(PRInt32 *x, PRInt32 *y)
281 : {
282 0 : nsIScrollableFrame* sf = GetScrollFrame();
283 0 : if (!sf)
284 0 : return NS_ERROR_FAILURE;
285 :
286 0 : nsPoint pt = sf->GetScrollPosition();
287 0 : *x = nsPresContext::AppUnitsToIntCSSPixels(pt.x);
288 0 : *y = nsPresContext::AppUnitsToIntCSSPixels(pt.y);
289 :
290 0 : return NS_OK;
291 : }
292 :
293 : /* void getScrolledSize (out long width, out long height); */
294 0 : NS_IMETHODIMP nsScrollBoxObject::GetScrolledSize(PRInt32 *width, PRInt32 *height)
295 : {
296 0 : nsIFrame* scrolledBox = GetScrolledBox(this);
297 0 : if (!scrolledBox)
298 0 : return NS_ERROR_FAILURE;
299 :
300 0 : nsRect scrollRect = scrolledBox->GetRect();
301 :
302 0 : *width = nsPresContext::AppUnitsToIntCSSPixels(scrollRect.width);
303 0 : *height = nsPresContext::AppUnitsToIntCSSPixels(scrollRect.height);
304 :
305 0 : return NS_OK;
306 : }
307 :
308 : /* void ensureElementIsVisible (in nsIDOMElement child); */
309 0 : NS_IMETHODIMP nsScrollBoxObject::EnsureElementIsVisible(nsIDOMElement *child)
310 : {
311 0 : NS_ENSURE_ARG_POINTER(child);
312 :
313 0 : nsCOMPtr<nsIPresShell> shell = GetPresShell(false);
314 0 : if (!shell) {
315 0 : return NS_ERROR_UNEXPECTED;
316 : }
317 :
318 0 : nsCOMPtr<nsIContent> content = do_QueryInterface(child);
319 0 : shell->ScrollContentIntoView(content,
320 : NS_PRESSHELL_SCROLL_ANYWHERE,
321 : NS_PRESSHELL_SCROLL_ANYWHERE,
322 : nsIPresShell::SCROLL_FIRST_ANCESTOR_ONLY |
323 0 : nsIPresShell::SCROLL_OVERFLOW_HIDDEN);
324 0 : return NS_OK;
325 : }
326 :
327 : /* void ensureIndexIsVisible (in long index); */
328 0 : NS_IMETHODIMP nsScrollBoxObject::EnsureIndexIsVisible(PRInt32 index)
329 : {
330 0 : return NS_ERROR_NOT_IMPLEMENTED;
331 : }
332 :
333 : /* void ensureLineIsVisible (in long line); */
334 0 : NS_IMETHODIMP nsScrollBoxObject::EnsureLineIsVisible(PRInt32 line)
335 : {
336 0 : return NS_ERROR_NOT_IMPLEMENTED;
337 : }
338 :
339 : nsresult
340 0 : NS_NewScrollBoxObject(nsIBoxObject** aResult)
341 : {
342 0 : *aResult = new nsScrollBoxObject;
343 0 : if (!*aResult)
344 0 : return NS_ERROR_OUT_OF_MEMORY;
345 0 : NS_ADDREF(*aResult);
346 0 : return NS_OK;
347 : }
348 :
|