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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
|
/* -*- 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/TreeBoxObject.h"
#include "nsCOMPtr.h"
#include "nsIDOMXULElement.h"
#include "nsIScriptableRegion.h"
#include "nsIXULTemplateBuilder.h"
#include "nsTreeContentView.h"
#include "nsITreeSelection.h"
#include "ChildIterator.h"
#include "nsContentUtils.h"
#include "nsError.h"
#include "nsTreeBodyFrame.h"
#include "mozilla/dom/TreeBoxObjectBinding.h"
#include "nsITreeColumns.h"
#include "mozilla/dom/DOMRect.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/ToJSValue.h"
namespace mozilla {
namespace dom {
NS_IMPL_CYCLE_COLLECTION_INHERITED(TreeBoxObject, BoxObject,
mView)
NS_IMPL_ADDREF_INHERITED(TreeBoxObject, BoxObject)
NS_IMPL_RELEASE_INHERITED(TreeBoxObject, BoxObject)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TreeBoxObject)
NS_INTERFACE_MAP_ENTRY(nsITreeBoxObject)
NS_INTERFACE_MAP_END_INHERITING(BoxObject)
void
TreeBoxObject::Clear()
{
ClearCachedValues();
// Drop the view's ref to us.
if (mView) {
nsCOMPtr<nsITreeSelection> sel;
mView->GetSelection(getter_AddRefs(sel));
if (sel)
sel->SetTree(nullptr);
mView->SetTree(nullptr); // Break the circular ref between the view and us.
}
mView = nullptr;
BoxObject::Clear();
}
TreeBoxObject::TreeBoxObject()
: mTreeBody(nullptr)
{
}
TreeBoxObject::~TreeBoxObject()
{
}
static nsIContent* FindBodyElement(nsIContent* aParent)
{
mozilla::dom::FlattenedChildIterator iter(aParent);
for (nsIContent* content = iter.GetNextChild(); content; content = iter.GetNextChild()) {
mozilla::dom::NodeInfo *ni = content->NodeInfo();
if (ni->Equals(nsGkAtoms::treechildren, kNameSpaceID_XUL)) {
return content;
} else if (ni->Equals(nsGkAtoms::tree, kNameSpaceID_XUL)) {
// There are nesting tree elements. Only the innermost should
// find the treechilren.
return nullptr;
} else if (content->IsElement() &&
!ni->Equals(nsGkAtoms::_template, kNameSpaceID_XUL)) {
nsIContent* result = FindBodyElement(content);
if (result)
return result;
}
}
return nullptr;
}
nsTreeBodyFrame*
TreeBoxObject::GetTreeBodyFrame(bool aFlushLayout)
{
// Make sure our frames are up to date, and layout as needed. We
// have to do this before checking for our cached mTreeBody, since
// it might go away on style flush, and in any case if aFlushLayout
// is true we need to make sure to flush no matter what.
// XXXbz except that flushing style when we were not asked to flush
// layout here breaks things. See bug 585123.
nsIFrame* frame = nullptr;
if (aFlushLayout) {
frame = GetFrame(aFlushLayout);
if (!frame)
return nullptr;
}
if (mTreeBody) {
// Have one cached already.
return mTreeBody;
}
if (!aFlushLayout) {
frame = GetFrame(aFlushLayout);
if (!frame)
return nullptr;
}
// Iterate over our content model children looking for the body.
nsCOMPtr<nsIContent> content = FindBodyElement(frame->GetContent());
if (!content)
return nullptr;
frame = content->GetPrimaryFrame();
if (!frame)
return nullptr;
// Make sure that the treebodyframe has a pointer to |this|.
nsTreeBodyFrame *treeBody = do_QueryFrame(frame);
NS_ENSURE_TRUE(treeBody && treeBody->GetTreeBoxObject() == this, nullptr);
mTreeBody = treeBody;
return mTreeBody;
}
NS_IMETHODIMP
TreeBoxObject::GetView(nsITreeView * *aView)
{
if (!mTreeBody) {
if (!GetTreeBodyFrame()) {
// Don't return an uninitialised view
*aView = nullptr;
return NS_OK;
}
if (mView)
// Our new frame needs to initialise itself
return mTreeBody->GetView(aView);
}
if (!mView) {
nsCOMPtr<nsIDOMXULElement> xulele = do_QueryInterface(mContent);
if (xulele) {
// See if there is a XUL tree builder associated with the element
nsCOMPtr<nsIXULTemplateBuilder> builder;
xulele->GetBuilder(getter_AddRefs(builder));
mView = do_QueryInterface(builder);
if (!mView) {
// No tree builder, create a tree content view.
nsresult rv = NS_NewTreeContentView(getter_AddRefs(mView));
NS_ENSURE_SUCCESS(rv, rv);
}
// Initialise the frame and view
mTreeBody->SetView(mView);
}
}
NS_IF_ADDREF(*aView = mView);
return NS_OK;
}
already_AddRefed<nsITreeView>
TreeBoxObject::GetView() {
nsCOMPtr<nsITreeView> view;
GetView(getter_AddRefs(view));
return view.forget();
}
static bool
CanTrustView(nsISupports* aValue)
{
// Untrusted content is only allowed to specify known-good views
if (nsContentUtils::IsCallerChrome())
return true;
nsCOMPtr<nsINativeTreeView> nativeTreeView = do_QueryInterface(aValue);
if (!nativeTreeView || NS_FAILED(nativeTreeView->EnsureNative())) {
// XXX ERRMSG need a good error here for developers
return false;
}
return true;
}
NS_IMETHODIMP TreeBoxObject::SetView(nsITreeView * aView)
{
if (!CanTrustView(aView))
return NS_ERROR_DOM_SECURITY_ERR;
mView = aView;
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
body->SetView(aView);
return NS_OK;
}
void TreeBoxObject::SetView(nsITreeView* aView, ErrorResult& aRv)
{
aRv = SetView(aView);
}
bool TreeBoxObject::Focused()
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->GetFocused();
return false;
}
NS_IMETHODIMP TreeBoxObject::GetFocused(bool* aFocused)
{
*aFocused = Focused();
return NS_OK;
}
NS_IMETHODIMP TreeBoxObject::SetFocused(bool aFocused)
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->SetFocused(aFocused);
return NS_OK;
}
NS_IMETHODIMP TreeBoxObject::GetTreeBody(nsIDOMElement** aElement)
{
*aElement = nullptr;
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->GetTreeBody(aElement);
return NS_OK;
}
already_AddRefed<Element>
TreeBoxObject::GetTreeBody()
{
nsCOMPtr<nsIDOMElement> el;
GetTreeBody(getter_AddRefs(el));
nsCOMPtr<Element> ret(do_QueryInterface(el));
return ret.forget();
}
already_AddRefed<nsTreeColumns>
TreeBoxObject::GetColumns()
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->Columns();
return nullptr;
}
NS_IMETHODIMP TreeBoxObject::GetColumns(nsITreeColumns** aColumns)
{
*aColumns = GetColumns().take();
return NS_OK;
}
int32_t TreeBoxObject::RowHeight()
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->RowHeight();
return 0;
}
int32_t TreeBoxObject::RowWidth()
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->RowWidth();
return 0;
}
NS_IMETHODIMP TreeBoxObject::GetRowHeight(int32_t* aRowHeight)
{
*aRowHeight = RowHeight();
return NS_OK;
}
NS_IMETHODIMP TreeBoxObject::GetRowWidth(int32_t *aRowWidth)
{
*aRowWidth = RowWidth();
return NS_OK;
}
int32_t TreeBoxObject::GetFirstVisibleRow()
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->FirstVisibleRow();
return 0;
}
NS_IMETHODIMP TreeBoxObject::GetFirstVisibleRow(int32_t *aFirstVisibleRow)
{
*aFirstVisibleRow = GetFirstVisibleRow();
return NS_OK;
}
int32_t TreeBoxObject::GetLastVisibleRow()
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->LastVisibleRow();
return 0;
}
NS_IMETHODIMP TreeBoxObject::GetLastVisibleRow(int32_t *aLastVisibleRow)
{
*aLastVisibleRow = GetLastVisibleRow();
return NS_OK;
}
int32_t TreeBoxObject::HorizontalPosition()
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->GetHorizontalPosition();
return 0;
}
NS_IMETHODIMP TreeBoxObject::GetHorizontalPosition(int32_t *aHorizontalPosition)
{
*aHorizontalPosition = HorizontalPosition();
return NS_OK;
}
int32_t TreeBoxObject::GetPageLength()
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->PageLength();
return 0;
}
NS_IMETHODIMP TreeBoxObject::GetPageLength(int32_t *aPageLength)
{
*aPageLength = GetPageLength();
return NS_OK;
}
NS_IMETHODIMP TreeBoxObject::GetSelectionRegion(nsIScriptableRegion **aRegion)
{
*aRegion = nullptr;
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->GetSelectionRegion(aRegion);
return NS_OK;
}
already_AddRefed<nsIScriptableRegion>
TreeBoxObject::SelectionRegion()
{
nsCOMPtr<nsIScriptableRegion> region;
GetSelectionRegion(getter_AddRefs(region));
return region.forget();
}
NS_IMETHODIMP
TreeBoxObject::EnsureRowIsVisible(int32_t aRow)
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->EnsureRowIsVisible(aRow);
return NS_OK;
}
NS_IMETHODIMP
TreeBoxObject::EnsureCellIsVisible(int32_t aRow, nsITreeColumn* aCol)
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->EnsureCellIsVisible(aRow, aCol);
return NS_OK;
}
NS_IMETHODIMP
TreeBoxObject::ScrollToRow(int32_t aRow)
{
nsTreeBodyFrame* body = GetTreeBodyFrame(true);
if (body)
return body->ScrollToRow(aRow);
return NS_OK;
}
NS_IMETHODIMP
TreeBoxObject::ScrollByLines(int32_t aNumLines)
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->ScrollByLines(aNumLines);
return NS_OK;
}
NS_IMETHODIMP
TreeBoxObject::ScrollByPages(int32_t aNumPages)
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->ScrollByPages(aNumPages);
return NS_OK;
}
NS_IMETHODIMP
TreeBoxObject::ScrollToCell(int32_t aRow, nsITreeColumn* aCol)
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->ScrollToCell(aRow, aCol);
return NS_OK;
}
NS_IMETHODIMP
TreeBoxObject::ScrollToColumn(nsITreeColumn* aCol)
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->ScrollToColumn(aCol);
return NS_OK;
}
NS_IMETHODIMP
TreeBoxObject::ScrollToHorizontalPosition(int32_t aHorizontalPosition)
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->ScrollToHorizontalPosition(aHorizontalPosition);
return NS_OK;
}
NS_IMETHODIMP TreeBoxObject::Invalidate()
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->Invalidate();
return NS_OK;
}
NS_IMETHODIMP
TreeBoxObject::InvalidateColumn(nsITreeColumn* aCol)
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->InvalidateColumn(aCol);
return NS_OK;
}
NS_IMETHODIMP
TreeBoxObject::InvalidateRow(int32_t aIndex)
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->InvalidateRow(aIndex);
return NS_OK;
}
NS_IMETHODIMP
TreeBoxObject::InvalidateCell(int32_t aRow, nsITreeColumn* aCol)
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->InvalidateCell(aRow, aCol);
return NS_OK;
}
NS_IMETHODIMP
TreeBoxObject::InvalidateRange(int32_t aStart, int32_t aEnd)
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->InvalidateRange(aStart, aEnd);
return NS_OK;
}
NS_IMETHODIMP
TreeBoxObject::InvalidateColumnRange(int32_t aStart, int32_t aEnd, nsITreeColumn* aCol)
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->InvalidateColumnRange(aStart, aEnd, aCol);
return NS_OK;
}
NS_IMETHODIMP
TreeBoxObject::GetRowAt(int32_t x, int32_t y, int32_t *aRow)
{
*aRow = 0;
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->GetRowAt(x, y, aRow);
return NS_OK;
}
int32_t
TreeBoxObject::GetRowAt(int32_t x, int32_t y)
{
int32_t row;
GetRowAt(x, y, &row);
return row;
}
NS_IMETHODIMP
TreeBoxObject::GetCellAt(int32_t aX, int32_t aY, int32_t *aRow,
nsITreeColumn** aCol, nsAString& aChildElt)
{
*aRow = 0;
*aCol = nullptr;
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body) {
nsAutoCString element;
nsresult retval = body->GetCellAt(aX, aY, aRow, aCol, element);
CopyUTF8toUTF16(element, aChildElt);
return retval;
}
return NS_OK;
}
void
TreeBoxObject::GetCellAt(int32_t x, int32_t y, TreeCellInfo& aRetVal, ErrorResult& aRv)
{
nsCOMPtr<nsITreeColumn> col;
GetCellAt(x, y, &aRetVal.mRow, getter_AddRefs(col), aRetVal.mChildElt);
aRetVal.mCol = col.forget().downcast<nsTreeColumn>();
}
void
TreeBoxObject::GetCellAt(JSContext* cx,
int32_t x, int32_t y,
JS::Handle<JSObject*> rowOut,
JS::Handle<JSObject*> colOut,
JS::Handle<JSObject*> childEltOut,
ErrorResult& aRv)
{
int32_t row;
nsITreeColumn* col;
nsAutoString childElt;
GetCellAt(x, y, &row, &col, childElt);
JS::Rooted<JS::Value> v(cx);
if (!ToJSValue(cx, row, &v) ||
!JS_SetProperty(cx, rowOut, "value", v)) {
aRv.Throw(NS_ERROR_XPC_CANT_SET_OUT_VAL);
return;
}
if (!dom::WrapObject(cx, col, &v) ||
!JS_SetProperty(cx, colOut, "value", v)) {
aRv.Throw(NS_ERROR_XPC_CANT_SET_OUT_VAL);
return;
}
if (!ToJSValue(cx, childElt, &v) ||
!JS_SetProperty(cx, childEltOut, "value", v)) {
aRv.Throw(NS_ERROR_XPC_CANT_SET_OUT_VAL);
return;
}
}
NS_IMETHODIMP
TreeBoxObject::GetCoordsForCellItem(int32_t aRow, nsITreeColumn* aCol, const nsAString& aElement,
int32_t *aX, int32_t *aY, int32_t *aWidth, int32_t *aHeight)
{
*aX = *aY = *aWidth = *aHeight = 0;
nsTreeBodyFrame* body = GetTreeBodyFrame();
NS_ConvertUTF16toUTF8 element(aElement);
if (body)
return body->GetCoordsForCellItem(aRow, aCol, element, aX, aY, aWidth, aHeight);
return NS_OK;
}
already_AddRefed<DOMRect>
TreeBoxObject::GetCoordsForCellItem(int32_t row, nsTreeColumn& col, const nsAString& element, ErrorResult& aRv)
{
int32_t x, y, w, h;
GetCoordsForCellItem(row, &col, element, &x, &y, &w, &h);
RefPtr<DOMRect> rect = new DOMRect(mContent, x, y, w, h);
return rect.forget();
}
void
TreeBoxObject::GetCoordsForCellItem(JSContext* cx,
int32_t row,
nsTreeColumn& col,
const nsAString& element,
JS::Handle<JSObject*> xOut,
JS::Handle<JSObject*> yOut,
JS::Handle<JSObject*> widthOut,
JS::Handle<JSObject*> heightOut,
ErrorResult& aRv)
{
int32_t x, y, w, h;
GetCoordsForCellItem(row, &col, element, &x, &y, &w, &h);
JS::Rooted<JS::Value> v(cx, JS::Int32Value(x));
if (!JS_SetProperty(cx, xOut, "value", v)) {
aRv.Throw(NS_ERROR_XPC_CANT_SET_OUT_VAL);
return;
}
v.setInt32(y);
if (!JS_SetProperty(cx, yOut, "value", v)) {
aRv.Throw(NS_ERROR_XPC_CANT_SET_OUT_VAL);
return;
}
v.setInt32(w);
if (!JS_SetProperty(cx, widthOut, "value", v)) {
aRv.Throw(NS_ERROR_XPC_CANT_SET_OUT_VAL);
return;
}
v.setInt32(h);
if (!JS_SetProperty(cx, heightOut, "value", v)) {
aRv.Throw(NS_ERROR_XPC_CANT_SET_OUT_VAL);
return;
}
}
NS_IMETHODIMP
TreeBoxObject::IsCellCropped(int32_t aRow, nsITreeColumn* aCol, bool *aIsCropped)
{
*aIsCropped = false;
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->IsCellCropped(aRow, aCol, aIsCropped);
return NS_OK;
}
bool
TreeBoxObject::IsCellCropped(int32_t row, nsITreeColumn* col, ErrorResult& aRv)
{
bool ret;
aRv = IsCellCropped(row, col, &ret);
return ret;
}
NS_IMETHODIMP
TreeBoxObject::RowCountChanged(int32_t aIndex, int32_t aDelta)
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->RowCountChanged(aIndex, aDelta);
return NS_OK;
}
NS_IMETHODIMP
TreeBoxObject::BeginUpdateBatch()
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->BeginUpdateBatch();
return NS_OK;
}
NS_IMETHODIMP
TreeBoxObject::EndUpdateBatch()
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->EndUpdateBatch();
return NS_OK;
}
NS_IMETHODIMP
TreeBoxObject::ClearStyleAndImageCaches()
{
nsTreeBodyFrame* body = GetTreeBodyFrame();
if (body)
return body->ClearStyleAndImageCaches();
return NS_OK;
}
void
TreeBoxObject::ClearCachedValues()
{
mTreeBody = nullptr;
}
JSObject*
TreeBoxObject::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
return TreeBoxObjectBinding::Wrap(aCx, this, aGivenProto);
}
} // namespace dom
} // namespace mozilla
// Creation Routine ///////////////////////////////////////////////////////////////////////
using namespace mozilla::dom;
nsresult
NS_NewTreeBoxObject(nsIBoxObject** aResult)
{
NS_ADDREF(*aResult = new TreeBoxObject());
return NS_OK;
}
|