summaryrefslogtreecommitdiffstats
path: root/layout/xul/ListBoxObject.cpp
blob: 435065f5b261bb6e9c160a24ac30eec17f1db297 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/ListBoxObject.h"
#include "nsCOMPtr.h"
#include "nsIFrame.h"
#include "nsGkAtoms.h"
#include "nsIScrollableFrame.h"
#include "nsListBoxBodyFrame.h"
#include "ChildIterator.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/ListBoxObjectBinding.h"

namespace mozilla {
namespace dom {

NS_IMPL_ISUPPORTS_INHERITED(ListBoxObject, BoxObject, nsIListBoxObject,
                            nsPIListBoxObject)

ListBoxObject::ListBoxObject()
  : mListBoxBody(nullptr)
{
}

ListBoxObject::~ListBoxObject()
{
}

JSObject* ListBoxObject::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
  return ListBoxObjectBinding::Wrap(aCx, this, aGivenProto);
}

// nsIListBoxObject
NS_IMETHODIMP
ListBoxObject::GetRowCount(int32_t *aResult)
{
  *aResult = GetRowCount();
  return NS_OK;
}

NS_IMETHODIMP
ListBoxObject::GetItemAtIndex(int32_t index, nsIDOMElement **_retval)
{
  nsListBoxBodyFrame* body = GetListBoxBody(true);
  if (body) {
    return body->GetItemAtIndex(index, _retval);
  }
  return NS_OK;
 }

NS_IMETHODIMP
ListBoxObject::GetIndexOfItem(nsIDOMElement* aElement, int32_t *aResult)
{
  *aResult = 0;

  nsListBoxBodyFrame* body = GetListBoxBody(true);
  if (body) {
    return body->GetIndexOfItem(aElement, aResult);
  }
  return NS_OK;
}

// ListBoxObject

int32_t
ListBoxObject::GetRowCount()
{
  nsListBoxBodyFrame* body = GetListBoxBody(true);
  if (body) {
    return body->GetRowCount();
  }
  return 0;
}

int32_t
ListBoxObject::GetNumberOfVisibleRows()
{
  nsListBoxBodyFrame* body = GetListBoxBody(true);
  if (body) {
    return body->GetNumberOfVisibleRows();
  }
  return 0;
}

int32_t
ListBoxObject::GetIndexOfFirstVisibleRow()
{
  nsListBoxBodyFrame* body = GetListBoxBody(true);
  if (body) {
    return body->GetIndexOfFirstVisibleRow();
  }
  return 0;
}

void
ListBoxObject::EnsureIndexIsVisible(int32_t aRowIndex)
{
  nsListBoxBodyFrame* body = GetListBoxBody(true);
  if (body) {
    body->EnsureIndexIsVisible(aRowIndex);
  }
}

void
ListBoxObject::ScrollToIndex(int32_t aRowIndex)
{
  nsListBoxBodyFrame* body = GetListBoxBody(true);
  if (body) {
    body->ScrollToIndex(aRowIndex);
  }
}

void
ListBoxObject::ScrollByLines(int32_t aNumLines)
{
  nsListBoxBodyFrame* body = GetListBoxBody(true);
  if (body) {
    body->ScrollByLines(aNumLines);
  }
}

already_AddRefed<Element>
ListBoxObject::GetItemAtIndex(int32_t index)
{
  nsCOMPtr<nsIDOMElement> el;
  GetItemAtIndex(index, getter_AddRefs(el));
  nsCOMPtr<Element> ret(do_QueryInterface(el));
  return ret.forget();
}

int32_t
ListBoxObject::GetIndexOfItem(Element& aElement)
{
  int32_t ret;
  nsCOMPtr<nsIDOMElement> el(do_QueryInterface(&aElement));
  GetIndexOfItem(el, &ret);
  return ret;
}

//////////////////////

static nsIContent*
FindBodyContent(nsIContent* aParent)
{
  if (aParent->IsXULElement(nsGkAtoms::listboxbody)) {
    return aParent;
  }

  mozilla::dom::FlattenedChildIterator iter(aParent);
  for (nsIContent* child = iter.GetNextChild(); child; child = iter.GetNextChild()) {
    nsIContent* result = FindBodyContent(child);
    if (result) {
      return result;
    }
  }

  return nullptr;
}

nsListBoxBodyFrame*
ListBoxObject::GetListBoxBody(bool aFlush)
{
  if (mListBoxBody) {
    return mListBoxBody;
  }

  nsIPresShell* shell = GetPresShell(false);
  if (!shell) {
    return nullptr;
  }

  nsIFrame* frame = aFlush ?
                      GetFrame(false) /* does Flush_Frames */ :
                      mContent->GetPrimaryFrame();
  if (!frame) {
    return nullptr;
  }

  // Iterate over our content model children looking for the body.
  nsCOMPtr<nsIContent> content = FindBodyContent(frame->GetContent());

  if (!content) {
    return nullptr;
  }

  // this frame will be a nsGFXScrollFrame
  frame = content->GetPrimaryFrame();
  if (!frame) {
     return nullptr;
  }

  nsIScrollableFrame* scrollFrame = do_QueryFrame(frame);
  if (!scrollFrame) {
    return nullptr;
  }

  // this frame will be the one we want
  nsIFrame* yeahBaby = scrollFrame->GetScrolledFrame();
  if (!yeahBaby) {
     return nullptr;
  }

  // It's a frame. Refcounts are irrelevant.
  nsListBoxBodyFrame* listBoxBody = do_QueryFrame(yeahBaby);
  NS_ENSURE_TRUE(listBoxBody &&
                 listBoxBody->SetBoxObject(this),
                 nullptr);
  mListBoxBody = listBoxBody;
  return mListBoxBody;
}

void
ListBoxObject::Clear()
{
  ClearCachedValues();
  BoxObject::Clear();
}

void
ListBoxObject::ClearCachedValues()
{
  mListBoxBody = nullptr;
}

} // namespace dom
} // namespace mozilla

// Creation Routine ///////////////////////////////////////////////////////////////////////

nsresult
NS_NewListBoxObject(nsIBoxObject** aResult)
{
  NS_ADDREF(*aResult = new mozilla::dom::ListBoxObject());
  return NS_OK;
}