Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
//
8
// Eric Vaughan
9
// Netscape Communications
10
//
11
// See documentation in associated header file
12
//
13
14
#include "nsScrollbarFrame.h"
15
#include "nsSliderFrame.h"
16
#include "nsScrollbarButtonFrame.h"
17
#include "nsContentCreatorFunctions.h"
18
#include "nsGkAtoms.h"
19
#include "nsIScrollableFrame.h"
20
#include "nsIScrollbarMediator.h"
21
#include "nsStyleConsts.h"
22
#include "nsIContent.h"
23
#include "mozilla/LookAndFeel.h"
24
#include "mozilla/PresShell.h"
25
#include "mozilla/dom/MutationEventBinding.h"
26
27
using namespace mozilla;
28
29
//
30
// NS_NewScrollbarFrame
31
//
32
// Creates a new scrollbar frame and returns it
33
//
34
nsIFrame* NS_NewScrollbarFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
35
return new (aPresShell)
36
nsScrollbarFrame(aStyle, aPresShell->GetPresContext());
37
}
38
39
NS_IMPL_FRAMEARENA_HELPERS(nsScrollbarFrame)
40
41
NS_QUERYFRAME_HEAD(nsScrollbarFrame)
42
NS_QUERYFRAME_ENTRY(nsScrollbarFrame)
43
NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
44
NS_QUERYFRAME_TAIL_INHERITING(nsBoxFrame)
45
46
void nsScrollbarFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
47
nsIFrame* aPrevInFlow) {
48
nsBoxFrame::Init(aContent, aParent, aPrevInFlow);
49
50
// We want to be a reflow root since we use reflows to move the
51
// slider. Any reflow inside the scrollbar frame will be a reflow to
52
// move the slider and will thus not change anything outside of the
53
// scrollbar or change the size of the scrollbar frame.
54
AddStateBits(NS_FRAME_REFLOW_ROOT);
55
}
56
57
void nsScrollbarFrame::DestroyFrom(nsIFrame* aDestructRoot,
58
PostDestroyData& aPostDestroyData) {
59
aPostDestroyData.AddAnonymousContent(mUpTopButton.forget());
60
aPostDestroyData.AddAnonymousContent(mDownTopButton.forget());
61
aPostDestroyData.AddAnonymousContent(mSlider.forget());
62
aPostDestroyData.AddAnonymousContent(mUpBottomButton.forget());
63
aPostDestroyData.AddAnonymousContent(mDownBottomButton.forget());
64
nsBoxFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
65
}
66
67
void nsScrollbarFrame::Reflow(nsPresContext* aPresContext,
68
ReflowOutput& aDesiredSize,
69
const ReflowInput& aReflowInput,
70
nsReflowStatus& aStatus) {
71
MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
72
73
nsBoxFrame::Reflow(aPresContext, aDesiredSize, aReflowInput, aStatus);
74
75
// nsGfxScrollFrame may have told us to shrink to nothing. If so, make sure
76
// our desired size agrees.
77
if (aReflowInput.AvailableWidth() == 0) {
78
aDesiredSize.Width() = 0;
79
}
80
if (aReflowInput.AvailableHeight() == 0) {
81
aDesiredSize.Height() = 0;
82
}
83
}
84
85
nsresult nsScrollbarFrame::AttributeChanged(int32_t aNameSpaceID,
86
nsAtom* aAttribute,
87
int32_t aModType) {
88
nsresult rv =
89
nsBoxFrame::AttributeChanged(aNameSpaceID, aAttribute, aModType);
90
91
// Update value in our children
92
UpdateChildrenAttributeValue(aAttribute, true);
93
94
// if the current position changes, notify any nsGfxScrollFrame
95
// parent we may have
96
if (aAttribute != nsGkAtoms::curpos) return rv;
97
98
nsIScrollableFrame* scrollable = do_QueryFrame(GetParent());
99
if (!scrollable) return rv;
100
101
nsCOMPtr<nsIContent> content(mContent);
102
scrollable->CurPosAttributeChanged(content);
103
return rv;
104
}
105
106
NS_IMETHODIMP
107
nsScrollbarFrame::HandlePress(nsPresContext* aPresContext,
108
WidgetGUIEvent* aEvent,
109
nsEventStatus* aEventStatus) {
110
return NS_OK;
111
}
112
113
NS_IMETHODIMP
114
nsScrollbarFrame::HandleMultiplePress(nsPresContext* aPresContext,
115
WidgetGUIEvent* aEvent,
116
nsEventStatus* aEventStatus,
117
bool aControlHeld) {
118
return NS_OK;
119
}
120
121
NS_IMETHODIMP
122
nsScrollbarFrame::HandleDrag(nsPresContext* aPresContext,
123
WidgetGUIEvent* aEvent,
124
nsEventStatus* aEventStatus) {
125
return NS_OK;
126
}
127
128
NS_IMETHODIMP
129
nsScrollbarFrame::HandleRelease(nsPresContext* aPresContext,
130
WidgetGUIEvent* aEvent,
131
nsEventStatus* aEventStatus) {
132
return NS_OK;
133
}
134
135
void nsScrollbarFrame::SetScrollbarMediatorContent(nsIContent* aMediator) {
136
mScrollbarMediator = aMediator;
137
}
138
139
nsIScrollbarMediator* nsScrollbarFrame::GetScrollbarMediator() {
140
if (!mScrollbarMediator) {
141
return nullptr;
142
}
143
nsIFrame* f = mScrollbarMediator->GetPrimaryFrame();
144
nsIScrollableFrame* scrollFrame = do_QueryFrame(f);
145
nsIScrollbarMediator* sbm;
146
147
if (scrollFrame) {
148
nsIFrame* scrolledFrame = scrollFrame->GetScrolledFrame();
149
sbm = do_QueryFrame(scrolledFrame);
150
if (sbm) {
151
return sbm;
152
}
153
}
154
sbm = do_QueryFrame(f);
155
if (f && !sbm) {
156
f = f->PresShell()->GetRootScrollFrame();
157
if (f && f->GetContent() == mScrollbarMediator) {
158
return do_QueryFrame(f);
159
}
160
}
161
return sbm;
162
}
163
164
nsresult nsScrollbarFrame::GetXULMargin(nsMargin& aMargin) {
165
nsresult rv = NS_ERROR_FAILURE;
166
aMargin.SizeTo(0, 0, 0, 0);
167
168
if (LookAndFeel::GetInt(LookAndFeel::eIntID_UseOverlayScrollbars) != 0) {
169
nsPresContext* presContext = PresContext();
170
nsITheme* theme = presContext->GetTheme();
171
if (theme && theme->ThemeSupportsWidget(presContext, this,
172
StyleAppearance::Scrollbar)) {
173
LayoutDeviceIntSize size;
174
bool isOverridable;
175
theme->GetMinimumWidgetSize(presContext, this, StyleAppearance::Scrollbar,
176
&size, &isOverridable);
177
if (IsXULHorizontal()) {
178
aMargin.top = -presContext->DevPixelsToAppUnits(size.height);
179
} else {
180
aMargin.left = -presContext->DevPixelsToAppUnits(size.width);
181
}
182
rv = NS_OK;
183
}
184
}
185
186
if (NS_FAILED(rv)) {
187
rv = nsBox::GetXULMargin(aMargin);
188
}
189
190
if (NS_SUCCEEDED(rv) && !IsXULHorizontal()) {
191
nsIScrollbarMediator* scrollFrame = GetScrollbarMediator();
192
if (scrollFrame && !scrollFrame->IsScrollbarOnRight()) {
193
Swap(aMargin.left, aMargin.right);
194
}
195
}
196
197
return rv;
198
}
199
200
void nsScrollbarFrame::SetIncrementToLine(int32_t aDirection) {
201
// get the scrollbar's content node
202
nsIContent* content = GetContent();
203
mSmoothScroll = true;
204
mIncrement = aDirection * nsSliderFrame::GetIncrement(content);
205
}
206
207
void nsScrollbarFrame::SetIncrementToPage(int32_t aDirection) {
208
// get the scrollbar's content node
209
nsIContent* content = GetContent();
210
mSmoothScroll = true;
211
mIncrement = aDirection * nsSliderFrame::GetPageIncrement(content);
212
}
213
214
void nsScrollbarFrame::SetIncrementToWhole(int32_t aDirection) {
215
// get the scrollbar's content node
216
nsIContent* content = GetContent();
217
if (aDirection == -1)
218
mIncrement = -nsSliderFrame::GetCurrentPosition(content);
219
else
220
mIncrement = nsSliderFrame::GetMaxPosition(content) -
221
nsSliderFrame::GetCurrentPosition(content);
222
// Don't repeat or use smooth scrolling if scrolling to beginning or end
223
// of a page.
224
mSmoothScroll = false;
225
}
226
227
int32_t nsScrollbarFrame::MoveToNewPosition() {
228
// get the scrollbar's content node
229
RefPtr<Element> content = GetContent()->AsElement();
230
231
// get the current pos
232
int32_t curpos = nsSliderFrame::GetCurrentPosition(content);
233
234
// get the max pos
235
int32_t maxpos = nsSliderFrame::GetMaxPosition(content);
236
237
// increment the given amount
238
if (mIncrement) {
239
curpos += mIncrement;
240
}
241
242
// make sure the current position is between the current and max positions
243
if (curpos < 0) {
244
curpos = 0;
245
} else if (curpos > maxpos) {
246
curpos = maxpos;
247
}
248
249
// set the current position of the slider.
250
nsAutoString curposStr;
251
curposStr.AppendInt(curpos);
252
253
AutoWeakFrame weakFrame(this);
254
if (mSmoothScroll) {
255
content->SetAttr(kNameSpaceID_None, nsGkAtoms::smooth,
256
NS_LITERAL_STRING("true"), false);
257
}
258
content->SetAttr(kNameSpaceID_None, nsGkAtoms::curpos, curposStr, false);
259
// notify the nsScrollbarFrame of the change
260
AttributeChanged(kNameSpaceID_None, nsGkAtoms::curpos,
261
dom::MutationEvent_Binding::MODIFICATION);
262
if (!weakFrame.IsAlive()) {
263
return curpos;
264
}
265
// notify all nsSliderFrames of the change
266
nsIFrame::ChildListIterator childLists(this);
267
for (; !childLists.IsDone(); childLists.Next()) {
268
nsFrameList::Enumerator childFrames(childLists.CurrentList());
269
for (; !childFrames.AtEnd(); childFrames.Next()) {
270
nsIFrame* f = childFrames.get();
271
nsSliderFrame* sliderFrame = do_QueryFrame(f);
272
if (sliderFrame) {
273
sliderFrame->AttributeChanged(kNameSpaceID_None, nsGkAtoms::curpos,
274
dom::MutationEvent_Binding::MODIFICATION);
275
if (!weakFrame.IsAlive()) {
276
return curpos;
277
}
278
}
279
}
280
}
281
content->UnsetAttr(kNameSpaceID_None, nsGkAtoms::smooth, false);
282
return curpos;
283
}
284
285
static already_AddRefed<Element> MakeScrollbarButton(
286
dom::NodeInfo* aNodeInfo, bool aVertical, bool aBottom, bool aDown,
287
AnonymousContentKey& aKey) {
288
MOZ_ASSERT(aNodeInfo);
289
MOZ_ASSERT(
290
aNodeInfo->Equals(nsGkAtoms::scrollbarbutton, nullptr, kNameSpaceID_XUL));
291
292
static constexpr nsLiteralString kSbattrValues[2][2] = {
293
{
294
NS_LITERAL_STRING("scrollbar-up-top"),
295
NS_LITERAL_STRING("scrollbar-up-bottom"),
296
},
297
{
298
NS_LITERAL_STRING("scrollbar-down-top"),
299
NS_LITERAL_STRING("scrollbar-down-bottom"),
300
},
301
};
302
303
static constexpr nsLiteralString kTypeValues[2] = {
304
NS_LITERAL_STRING("decrement"),
305
NS_LITERAL_STRING("increment"),
306
};
307
308
aKey = AnonymousContentKey::Type_ScrollbarButton;
309
if (aVertical) {
310
aKey |= AnonymousContentKey::Flag_Vertical;
311
}
312
if (aBottom) {
313
aKey |= AnonymousContentKey::Flag_ScrollbarButton_Bottom;
314
}
315
if (aDown) {
316
aKey |= AnonymousContentKey::Flag_ScrollbarButton_Down;
317
}
318
319
RefPtr<Element> e;
320
NS_TrustedNewXULElement(getter_AddRefs(e), do_AddRef(aNodeInfo));
321
e->SetAttr(kNameSpaceID_None, nsGkAtoms::sbattr,
322
kSbattrValues[aDown][aBottom], false);
323
e->SetAttr(kNameSpaceID_None, nsGkAtoms::type, kTypeValues[aDown], false);
324
return e.forget();
325
}
326
327
nsresult nsScrollbarFrame::CreateAnonymousContent(
328
nsTArray<ContentInfo>& aElements) {
329
nsNodeInfoManager* nodeInfoManager = mContent->NodeInfo()->NodeInfoManager();
330
331
Element* el(GetContent()->AsElement());
332
333
// If there are children already in the node, don't create any anonymous
334
// content (this only apply to crashtests/369038-1.xhtml)
335
if (el->HasChildren()) {
336
return NS_OK;
337
}
338
339
nsAutoString orient;
340
el->GetAttr(kNameSpaceID_None, nsGkAtoms::orient, orient);
341
bool vertical = orient.EqualsLiteral("vertical");
342
343
RefPtr<dom::NodeInfo> sbbNodeInfo =
344
nodeInfoManager->GetNodeInfo(nsGkAtoms::scrollbarbutton, nullptr,
345
kNameSpaceID_XUL, nsINode::ELEMENT_NODE);
346
347
{
348
AnonymousContentKey key;
349
mUpTopButton =
350
MakeScrollbarButton(sbbNodeInfo, vertical, /* aBottom */ false,
351
/* aDown */ false, key);
352
aElements.AppendElement(ContentInfo(mUpTopButton, key));
353
}
354
355
{
356
AnonymousContentKey key;
357
mDownTopButton =
358
MakeScrollbarButton(sbbNodeInfo, vertical, /* aBottom */ false,
359
/* aDown */ true, key);
360
aElements.AppendElement(ContentInfo(mDownTopButton, key));
361
}
362
363
{
364
AnonymousContentKey key = AnonymousContentKey::Type_Slider;
365
if (vertical) {
366
key |= AnonymousContentKey::Flag_Vertical;
367
}
368
369
NS_TrustedNewXULElement(
370
getter_AddRefs(mSlider),
371
nodeInfoManager->GetNodeInfo(nsGkAtoms::slider, nullptr,
372
kNameSpaceID_XUL, nsINode::ELEMENT_NODE));
373
mSlider->SetAttr(kNameSpaceID_None, nsGkAtoms::orient, orient, false);
374
mSlider->SetAttr(kNameSpaceID_None, nsGkAtoms::flex, NS_LITERAL_STRING("1"),
375
false);
376
377
aElements.AppendElement(ContentInfo(mSlider, key));
378
379
NS_TrustedNewXULElement(
380
getter_AddRefs(mThumb),
381
nodeInfoManager->GetNodeInfo(nsGkAtoms::thumb, nullptr,
382
kNameSpaceID_XUL, nsINode::ELEMENT_NODE));
383
mThumb->SetAttr(kNameSpaceID_None, nsGkAtoms::orient, orient, false);
384
mThumb->SetAttr(kNameSpaceID_None, nsGkAtoms::align,
385
NS_LITERAL_STRING("center"), false);
386
mThumb->SetAttr(kNameSpaceID_None, nsGkAtoms::pack,
387
NS_LITERAL_STRING("center"), false);
388
mSlider->AppendChildTo(mThumb, false);
389
}
390
391
{
392
AnonymousContentKey key;
393
mUpBottomButton =
394
MakeScrollbarButton(sbbNodeInfo, vertical, /* aBottom */ true,
395
/* aDown */ false, key);
396
aElements.AppendElement(ContentInfo(mUpBottomButton, key));
397
}
398
399
{
400
AnonymousContentKey key;
401
mDownBottomButton =
402
MakeScrollbarButton(sbbNodeInfo, vertical, /* aBottom */ true,
403
/* aDown */ true, key);
404
aElements.AppendElement(ContentInfo(mDownBottomButton, key));
405
}
406
407
// Don't cache styles if we are inside a <select> element, since we have
408
// some UA style sheet rules that depend on the <select>'s attributes.
409
if (GetContent()->GetParent() &&
410
GetContent()->GetParent()->IsHTMLElement(nsGkAtoms::select)) {
411
for (auto& info : aElements) {
412
info.mKey = AnonymousContentKey::None;
413
}
414
}
415
416
UpdateChildrenAttributeValue(nsGkAtoms::curpos, false);
417
UpdateChildrenAttributeValue(nsGkAtoms::maxpos, false);
418
UpdateChildrenAttributeValue(nsGkAtoms::disabled, false);
419
UpdateChildrenAttributeValue(nsGkAtoms::pageincrement, false);
420
UpdateChildrenAttributeValue(nsGkAtoms::increment, false);
421
422
return NS_OK;
423
}
424
425
void nsScrollbarFrame::UpdateChildrenAttributeValue(nsAtom* aAttribute,
426
bool aNotify) {
427
Element* el(GetContent()->AsElement());
428
429
nsAutoString value;
430
el->GetAttr(kNameSpaceID_None, aAttribute, value);
431
432
if (!el->HasAttr(kNameSpaceID_None, aAttribute)) {
433
if (mUpTopButton) {
434
mUpTopButton->UnsetAttr(kNameSpaceID_None, aAttribute, aNotify);
435
}
436
if (mDownTopButton) {
437
mDownTopButton->UnsetAttr(kNameSpaceID_None, aAttribute, aNotify);
438
}
439
if (mSlider) {
440
mSlider->UnsetAttr(kNameSpaceID_None, aAttribute, aNotify);
441
}
442
if (mThumb && aAttribute == nsGkAtoms::disabled) {
443
mThumb->UnsetAttr(kNameSpaceID_None, nsGkAtoms::collapsed, aNotify);
444
}
445
if (mUpBottomButton) {
446
mUpBottomButton->UnsetAttr(kNameSpaceID_None, aAttribute, aNotify);
447
}
448
if (mDownBottomButton) {
449
mDownBottomButton->UnsetAttr(kNameSpaceID_None, aAttribute, aNotify);
450
}
451
return;
452
}
453
454
if (aAttribute == nsGkAtoms::curpos || aAttribute == nsGkAtoms::maxpos) {
455
if (mUpTopButton) {
456
mUpTopButton->SetAttr(kNameSpaceID_None, aAttribute, value, aNotify);
457
}
458
if (mDownTopButton) {
459
mDownTopButton->SetAttr(kNameSpaceID_None, aAttribute, value, aNotify);
460
}
461
if (mSlider) {
462
mSlider->SetAttr(kNameSpaceID_None, aAttribute, value, aNotify);
463
}
464
if (mUpBottomButton) {
465
mUpBottomButton->SetAttr(kNameSpaceID_None, aAttribute, value, aNotify);
466
}
467
if (mDownBottomButton) {
468
mDownBottomButton->SetAttr(kNameSpaceID_None, aAttribute, value, aNotify);
469
}
470
} else if (aAttribute == nsGkAtoms::disabled) {
471
if (mUpTopButton) {
472
mUpTopButton->SetAttr(kNameSpaceID_None, aAttribute, value, aNotify);
473
}
474
if (mDownTopButton) {
475
mDownTopButton->SetAttr(kNameSpaceID_None, aAttribute, value, aNotify);
476
}
477
if (mSlider) {
478
mSlider->SetAttr(kNameSpaceID_None, aAttribute, value, aNotify);
479
}
480
// Set the value on "collapsed" attribute.
481
if (mThumb) {
482
mThumb->SetAttr(kNameSpaceID_None, nsGkAtoms::collapsed, value, aNotify);
483
}
484
if (mUpBottomButton) {
485
mUpBottomButton->SetAttr(kNameSpaceID_None, aAttribute, value, aNotify);
486
}
487
if (mDownBottomButton) {
488
mDownBottomButton->SetAttr(kNameSpaceID_None, aAttribute, value, aNotify);
489
}
490
} else if (aAttribute == nsGkAtoms::pageincrement ||
491
aAttribute == nsGkAtoms::increment) {
492
if (mSlider) {
493
mSlider->SetAttr(kNameSpaceID_None, aAttribute, value, aNotify);
494
}
495
}
496
}
497
498
void nsScrollbarFrame::AppendAnonymousContentTo(
499
nsTArray<nsIContent*>& aElements, uint32_t aFilter) {
500
if (mUpTopButton) {
501
aElements.AppendElement(mUpTopButton);
502
}
503
504
if (mDownTopButton) {
505
aElements.AppendElement(mDownTopButton);
506
}
507
508
if (mSlider) {
509
aElements.AppendElement(mSlider);
510
}
511
512
if (mUpBottomButton) {
513
aElements.AppendElement(mUpBottomButton);
514
}
515
516
if (mDownBottomButton) {
517
aElements.AppendElement(mDownBottomButton);
518
}
519
}