~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/layout/xul/base/src/nsListBoxObject.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Netscape Public License
 
6
 * Version 1.1 (the "License"); you may not use this file except in
 
7
 * compliance with the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/NPL/
 
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 the GNU General Public License Version 2 or later (the "GPL"), or 
 
28
 * 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 NPL, 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 NPL, the GPL or the LGPL.
 
37
 *
 
38
 * ***** END LICENSE BLOCK ***** */
 
39
 
 
40
#include "nsCOMPtr.h"
 
41
#include "nsIListBoxObject.h"
 
42
#include "nsBoxObject.h"
 
43
#include "nsIFrame.h"
 
44
#include "nsIDocument.h"
 
45
#include "nsIBindingManager.h"
 
46
#include "nsIDOMElement.h"
 
47
#include "nsIDOMNodeList.h"
 
48
#include "nsXULAtoms.h"
 
49
 
 
50
class nsListBoxObject : public nsIListBoxObject, public nsBoxObject
 
51
{
 
52
public:
 
53
  NS_DECL_ISUPPORTS
 
54
  NS_DECL_NSILISTBOXOBJECT
 
55
 
 
56
  nsListBoxObject();
 
57
  virtual ~nsListBoxObject();
 
58
 
 
59
  nsIListBoxObject* GetListBoxBody();
 
60
 
 
61
  NS_IMETHOD InvalidatePresentationStuff();
 
62
  
 
63
protected:
 
64
};
 
65
 
 
66
NS_IMPL_ADDREF(nsListBoxObject)
 
67
NS_IMPL_RELEASE(nsListBoxObject)
 
68
 
 
69
NS_IMETHODIMP 
 
70
nsListBoxObject::QueryInterface(REFNSIID iid, void** aResult)
 
71
{
 
72
  if (!aResult)
 
73
    return NS_ERROR_NULL_POINTER;
 
74
  
 
75
  if (iid.Equals(NS_GET_IID(nsIListBoxObject))) {
 
76
    *aResult = (nsIListBoxObject*)this;
 
77
    NS_ADDREF(this);
 
78
    return NS_OK;
 
79
  }
 
80
 
 
81
  return nsBoxObject::QueryInterface(iid, aResult);
 
82
}
 
83
  
 
84
nsListBoxObject::nsListBoxObject()
 
85
{
 
86
}
 
87
 
 
88
nsListBoxObject::~nsListBoxObject()
 
89
{
 
90
}
 
91
 
 
92
 
 
93
//////////////////////////////////////////////////////////////////////////
 
94
//// nsIListBoxObject
 
95
 
 
96
NS_IMETHODIMP
 
97
nsListBoxObject::GetListboxBody(nsIListBoxObject * *aListboxBody)
 
98
{
 
99
  *aListboxBody = GetListBoxBody();
 
100
  NS_IF_ADDREF(*aListboxBody);
 
101
  return NS_OK;
 
102
}
 
103
 
 
104
NS_IMETHODIMP
 
105
nsListBoxObject::GetRowCount(PRInt32 *aResult)
 
106
{
 
107
  nsIListBoxObject* body = GetListBoxBody();
 
108
  if (body)
 
109
    return body->GetRowCount(aResult);
 
110
  return NS_OK;
 
111
}
 
112
 
 
113
NS_IMETHODIMP
 
114
nsListBoxObject::GetNumberOfVisibleRows(PRInt32 *aResult)
 
115
{
 
116
  nsIListBoxObject* body = GetListBoxBody();
 
117
  if (body)
 
118
    return body->GetNumberOfVisibleRows(aResult);
 
119
  return NS_OK;
 
120
}
 
121
 
 
122
NS_IMETHODIMP
 
123
nsListBoxObject::GetIndexOfFirstVisibleRow(PRInt32 *aResult)
 
124
{
 
125
  nsIListBoxObject* body = GetListBoxBody();
 
126
  if (body)
 
127
    return body->GetIndexOfFirstVisibleRow(aResult);
 
128
  return NS_OK;
 
129
}
 
130
 
 
131
NS_IMETHODIMP nsListBoxObject::EnsureIndexIsVisible(PRInt32 aRowIndex)
 
132
{
 
133
  nsIListBoxObject* body = GetListBoxBody();
 
134
  if (body)
 
135
    return body->EnsureIndexIsVisible(aRowIndex);
 
136
  return NS_OK;
 
137
}
 
138
 
 
139
NS_IMETHODIMP
 
140
nsListBoxObject::ScrollToIndex(PRInt32 aRowIndex)
 
141
{
 
142
  nsIListBoxObject* body = GetListBoxBody();
 
143
  if (body)
 
144
    return body->ScrollToIndex(aRowIndex);
 
145
  return NS_OK;
 
146
}
 
147
 
 
148
NS_IMETHODIMP
 
149
nsListBoxObject::ScrollByLines(PRInt32 aNumLines)
 
150
{
 
151
  nsIListBoxObject* body = GetListBoxBody();
 
152
  if (body)
 
153
    return body->ScrollByLines(aNumLines);
 
154
  return NS_OK;
 
155
}
 
156
 
 
157
NS_IMETHODIMP
 
158
nsListBoxObject::GetItemAtIndex(PRInt32 index, nsIDOMElement **_retval)
 
159
{
 
160
  nsIListBoxObject* body = GetListBoxBody();
 
161
  if (body)
 
162
    return body->GetItemAtIndex(index, _retval);
 
163
  return NS_OK;
 
164
}
 
165
 
 
166
NS_IMETHODIMP
 
167
nsListBoxObject::GetIndexOfItem(nsIDOMElement* aElement, PRInt32 *aResult)
 
168
{
 
169
  *aResult = 0;
 
170
 
 
171
  nsIListBoxObject* body = GetListBoxBody();
 
172
  if (body)
 
173
    return body->GetIndexOfItem(aElement, aResult);
 
174
  return NS_OK;
 
175
}
 
176
 
 
177
//////////////////////
 
178
 
 
179
static void
 
180
FindBodyContent(nsIContent* aParent, nsIContent** aResult)
 
181
{
 
182
  if (aParent->Tag() == nsXULAtoms::listboxbody) {
 
183
    *aResult = aParent;
 
184
    NS_IF_ADDREF(*aResult);
 
185
  }
 
186
  else {
 
187
    nsCOMPtr<nsIDOMNodeList> kids;
 
188
    aParent->GetDocument()->GetBindingManager()->GetXBLChildNodesFor(aParent, getter_AddRefs(kids));
 
189
    if (!kids) return;
 
190
 
 
191
    PRUint32 i;
 
192
    kids->GetLength(&i);
 
193
    // start from the end, cuz we're smart and we know the listboxbody is probably at the end
 
194
    while (i > 0) {
 
195
      nsCOMPtr<nsIDOMNode> childNode;
 
196
      kids->Item(--i, getter_AddRefs(childNode));
 
197
      nsCOMPtr<nsIContent> childContent(do_QueryInterface(childNode));
 
198
      FindBodyContent(childContent, aResult);
 
199
      if (*aResult)
 
200
        break;
 
201
    }
 
202
  }
 
203
}
 
204
 
 
205
nsIListBoxObject*
 
206
nsListBoxObject::GetListBoxBody()
 
207
{
 
208
  NS_NAMED_LITERAL_STRING(listboxbody, "listboxbody");
 
209
 
 
210
  nsCOMPtr<nsISupports> supp;
 
211
  GetPropertyAsSupports(listboxbody.get(), getter_AddRefs(supp));
 
212
 
 
213
  if (supp) {
 
214
    nsCOMPtr<nsIListBoxObject> body(do_QueryInterface(supp));
 
215
    return body;
 
216
  }
 
217
 
 
218
  nsIFrame* frame = GetFrame();
 
219
  if (!frame)
 
220
    return nsnull;
 
221
 
 
222
  // Iterate over our content model children looking for the body.
 
223
  nsCOMPtr<nsIContent> content;
 
224
  FindBodyContent(frame->GetContent(), getter_AddRefs(content));
 
225
 
 
226
  // this frame will be a nsGFXScrollFrame
 
227
  mPresShell->GetPrimaryFrameFor(content, &frame);
 
228
  if (!frame)
 
229
     return nsnull;
 
230
 
 
231
  // this frame will be a nsListBoxScrollPortFrame
 
232
  nsIFrame* scrollPort = frame->GetFirstChild(nsnull);
 
233
  if (!scrollPort)
 
234
     return nsnull;
 
235
 
 
236
  // this frame will be the one we want
 
237
  nsIFrame* yeahBaby = scrollPort->GetFirstChild(nsnull);
 
238
  if (!yeahBaby)
 
239
     return nsnull;
 
240
 
 
241
  // It's a frame. Refcounts are irrelevant.
 
242
  nsCOMPtr<nsIListBoxObject> body;
 
243
  yeahBaby->QueryInterface(NS_GET_IID(nsIListBoxObject), getter_AddRefs(body));
 
244
  SetPropertyAsSupports(listboxbody.get(), body);
 
245
  return body;
 
246
}
 
247
 
 
248
NS_IMETHODIMP
 
249
nsListBoxObject::InvalidatePresentationStuff()
 
250
{
 
251
  SetPropertyAsSupports(NS_LITERAL_STRING("listboxbody").get(), nsnull);
 
252
 
 
253
  return nsBoxObject::InvalidatePresentationStuff();
 
254
}
 
255
 
 
256
// Creation Routine ///////////////////////////////////////////////////////////////////////
 
257
 
 
258
nsresult
 
259
NS_NewListBoxObject(nsIBoxObject** aResult)
 
260
{
 
261
  *aResult = new nsListBoxObject;
 
262
  if (!*aResult)
 
263
    return NS_ERROR_OUT_OF_MEMORY;
 
264
  NS_ADDREF(*aResult);
 
265
  return NS_OK;
 
266
}