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 : * David W. Hyatt (hyatt@netscape.com) (Original Author)
24 : * Joe Hewitt (hewitt@netscape.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 "nsPIListBoxObject.h"
42 : #include "nsBoxObject.h"
43 : #include "nsIFrame.h"
44 : #include "nsIDocument.h"
45 : #include "nsBindingManager.h"
46 : #include "nsIDOMElement.h"
47 : #include "nsIDOMNodeList.h"
48 : #include "nsGkAtoms.h"
49 : #include "nsIScrollableFrame.h"
50 : #include "nsListBoxBodyFrame.h"
51 :
52 : class nsListBoxObject : public nsPIListBoxObject, public nsBoxObject
53 0 : {
54 : public:
55 : NS_DECL_ISUPPORTS_INHERITED
56 : NS_DECL_NSILISTBOXOBJECT
57 :
58 : // nsPIListBoxObject
59 : virtual nsListBoxBodyFrame* GetListBoxBody(bool aFlush);
60 :
61 : nsListBoxObject();
62 :
63 : // nsPIBoxObject
64 : virtual void Clear();
65 : virtual void ClearCachedValues();
66 :
67 : protected:
68 : nsListBoxBodyFrame *mListBoxBody;
69 : };
70 :
71 0 : NS_IMPL_ISUPPORTS_INHERITED2(nsListBoxObject, nsBoxObject, nsIListBoxObject,
72 : nsPIListBoxObject)
73 :
74 0 : nsListBoxObject::nsListBoxObject()
75 0 : : mListBoxBody(nsnull)
76 : {
77 0 : }
78 :
79 : //////////////////////////////////////////////////////////////////////////
80 : //// nsIListBoxObject
81 :
82 : NS_IMETHODIMP
83 0 : nsListBoxObject::GetRowCount(PRInt32 *aResult)
84 : {
85 0 : nsListBoxBodyFrame* body = GetListBoxBody(true);
86 0 : if (body)
87 0 : return body->GetRowCount(aResult);
88 0 : return NS_OK;
89 : }
90 :
91 : NS_IMETHODIMP
92 0 : nsListBoxObject::GetNumberOfVisibleRows(PRInt32 *aResult)
93 : {
94 0 : nsListBoxBodyFrame* body = GetListBoxBody(true);
95 0 : if (body)
96 0 : return body->GetNumberOfVisibleRows(aResult);
97 0 : return NS_OK;
98 : }
99 :
100 : NS_IMETHODIMP
101 0 : nsListBoxObject::GetIndexOfFirstVisibleRow(PRInt32 *aResult)
102 : {
103 0 : nsListBoxBodyFrame* body = GetListBoxBody(true);
104 0 : if (body)
105 0 : return body->GetIndexOfFirstVisibleRow(aResult);
106 0 : return NS_OK;
107 : }
108 :
109 0 : NS_IMETHODIMP nsListBoxObject::EnsureIndexIsVisible(PRInt32 aRowIndex)
110 : {
111 0 : nsListBoxBodyFrame* body = GetListBoxBody(true);
112 0 : if (body)
113 0 : return body->EnsureIndexIsVisible(aRowIndex);
114 0 : return NS_OK;
115 : }
116 :
117 : NS_IMETHODIMP
118 0 : nsListBoxObject::ScrollToIndex(PRInt32 aRowIndex)
119 : {
120 0 : nsListBoxBodyFrame* body = GetListBoxBody(true);
121 0 : if (body)
122 0 : return body->ScrollToIndex(aRowIndex);
123 0 : return NS_OK;
124 : }
125 :
126 : NS_IMETHODIMP
127 0 : nsListBoxObject::ScrollByLines(PRInt32 aNumLines)
128 : {
129 0 : nsListBoxBodyFrame* body = GetListBoxBody(true);
130 0 : if (body)
131 0 : return body->ScrollByLines(aNumLines);
132 0 : return NS_OK;
133 : }
134 :
135 : NS_IMETHODIMP
136 0 : nsListBoxObject::GetItemAtIndex(PRInt32 index, nsIDOMElement **_retval)
137 : {
138 0 : nsListBoxBodyFrame* body = GetListBoxBody(true);
139 0 : if (body)
140 0 : return body->GetItemAtIndex(index, _retval);
141 0 : return NS_OK;
142 : }
143 :
144 : NS_IMETHODIMP
145 0 : nsListBoxObject::GetIndexOfItem(nsIDOMElement* aElement, PRInt32 *aResult)
146 : {
147 0 : *aResult = 0;
148 :
149 0 : nsListBoxBodyFrame* body = GetListBoxBody(true);
150 0 : if (body)
151 0 : return body->GetIndexOfItem(aElement, aResult);
152 0 : return NS_OK;
153 : }
154 :
155 : //////////////////////
156 :
157 : static void
158 0 : FindBodyContent(nsIContent* aParent, nsIContent** aResult)
159 : {
160 0 : if (aParent->Tag() == nsGkAtoms::listboxbody) {
161 0 : *aResult = aParent;
162 0 : NS_IF_ADDREF(*aResult);
163 : }
164 : else {
165 0 : nsCOMPtr<nsIDOMNodeList> kids;
166 0 : aParent->OwnerDoc()->BindingManager()->GetXBLChildNodesFor(aParent, getter_AddRefs(kids));
167 0 : if (!kids) return;
168 :
169 : PRUint32 i;
170 0 : kids->GetLength(&i);
171 : // start from the end, cuz we're smart and we know the listboxbody is probably at the end
172 0 : while (i > 0) {
173 0 : nsCOMPtr<nsIDOMNode> childNode;
174 0 : kids->Item(--i, getter_AddRefs(childNode));
175 0 : nsCOMPtr<nsIContent> childContent(do_QueryInterface(childNode));
176 0 : FindBodyContent(childContent, aResult);
177 0 : if (*aResult)
178 : break;
179 : }
180 : }
181 : }
182 :
183 : nsListBoxBodyFrame*
184 0 : nsListBoxObject::GetListBoxBody(bool aFlush)
185 : {
186 0 : if (mListBoxBody) {
187 0 : return mListBoxBody;
188 : }
189 :
190 0 : nsIPresShell* shell = GetPresShell(false);
191 0 : if (!shell) {
192 0 : return nsnull;
193 : }
194 :
195 : nsIFrame* frame = aFlush ?
196 0 : GetFrame(false) /* does Flush_Frames */ :
197 0 : mContent->GetPrimaryFrame();
198 0 : if (!frame)
199 0 : return nsnull;
200 :
201 : // Iterate over our content model children looking for the body.
202 0 : nsCOMPtr<nsIContent> content;
203 0 : FindBodyContent(frame->GetContent(), getter_AddRefs(content));
204 :
205 0 : if (!content)
206 0 : return nsnull;
207 :
208 : // this frame will be a nsGFXScrollFrame
209 0 : frame = content->GetPrimaryFrame();
210 0 : if (!frame)
211 0 : return nsnull;
212 0 : nsIScrollableFrame* scrollFrame = do_QueryFrame(frame);
213 0 : if (!scrollFrame)
214 0 : return nsnull;
215 :
216 : // this frame will be the one we want
217 0 : nsIFrame* yeahBaby = scrollFrame->GetScrolledFrame();
218 0 : if (!yeahBaby)
219 0 : return nsnull;
220 :
221 : // It's a frame. Refcounts are irrelevant.
222 0 : nsListBoxBodyFrame* listBoxBody = do_QueryFrame(yeahBaby);
223 0 : NS_ENSURE_TRUE(listBoxBody &&
224 : listBoxBody->SetBoxObject(this),
225 : nsnull);
226 0 : mListBoxBody = listBoxBody;
227 0 : return mListBoxBody;
228 : }
229 :
230 : void
231 0 : nsListBoxObject::Clear()
232 : {
233 0 : ClearCachedValues();
234 :
235 0 : nsBoxObject::Clear();
236 0 : }
237 :
238 : void
239 0 : nsListBoxObject::ClearCachedValues()
240 : {
241 0 : mListBoxBody = nsnull;
242 0 : }
243 :
244 : // Creation Routine ///////////////////////////////////////////////////////////////////////
245 :
246 : nsresult
247 0 : NS_NewListBoxObject(nsIBoxObject** aResult)
248 : {
249 0 : *aResult = new nsListBoxObject;
250 0 : if (!*aResult)
251 0 : return NS_ERROR_OUT_OF_MEMORY;
252 0 : NS_ADDREF(*aResult);
253 0 : return NS_OK;
254 : }
|