forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nsHTMLButtonControlFrame.cpp
383 lines (318 loc) · 12.4 KB
/
nsHTMLButtonControlFrame.cpp
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
/* -*- 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 "nsHTMLButtonControlFrame.h"
#include "nsCOMPtr.h"
#include "nsContainerFrame.h"
#include "nsIFormControlFrame.h"
#include "nsHTMLParts.h"
#include "nsIFormControl.h"
#include "nsPresContext.h"
#include "nsIPresShell.h"
#include "nsStyleContext.h"
#include "nsLeafFrame.h"
#include "nsCSSRendering.h"
#include "nsISupports.h"
#include "nsGkAtoms.h"
#include "nsCSSAnonBoxes.h"
#include "nsStyleConsts.h"
#include "nsIComponentManager.h"
#include "nsButtonFrameRenderer.h"
#include "nsFormControlFrame.h"
#include "nsFrameManager.h"
#include "nsINameSpaceManager.h"
#include "nsIServiceManager.h"
#include "nsIDOMHTMLButtonElement.h"
#include "nsIDOMHTMLInputElement.h"
#include "nsStyleSet.h"
#include "nsDisplayList.h"
using namespace mozilla;
nsIFrame*
NS_NewHTMLButtonControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
{
return new (aPresShell) nsHTMLButtonControlFrame(aContext);
}
NS_IMPL_FRAMEARENA_HELPERS(nsHTMLButtonControlFrame)
nsHTMLButtonControlFrame::nsHTMLButtonControlFrame(nsStyleContext* aContext)
: nsContainerFrame(aContext)
{
}
nsHTMLButtonControlFrame::~nsHTMLButtonControlFrame()
{
}
void
nsHTMLButtonControlFrame::DestroyFrom(nsIFrame* aDestructRoot)
{
nsFormControlFrame::RegUnRegAccessKey(static_cast<nsIFrame*>(this), false);
nsContainerFrame::DestroyFrom(aDestructRoot);
}
NS_IMETHODIMP
nsHTMLButtonControlFrame::Init(
nsIContent* aContent,
nsIFrame* aParent,
nsIFrame* aPrevInFlow)
{
nsresult rv = nsContainerFrame::Init(aContent, aParent, aPrevInFlow);
if (NS_SUCCEEDED(rv)) {
mRenderer.SetFrame(this, PresContext());
}
return rv;
}
NS_QUERYFRAME_HEAD(nsHTMLButtonControlFrame)
NS_QUERYFRAME_ENTRY(nsIFormControlFrame)
NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
#ifdef ACCESSIBILITY
a11y::AccType
nsHTMLButtonControlFrame::AccessibleType()
{
return a11y::eHTMLButtonType;
}
#endif
nsIAtom*
nsHTMLButtonControlFrame::GetType() const
{
return nsGkAtoms::HTMLButtonControlFrame;
}
void
nsHTMLButtonControlFrame::SetFocus(bool aOn, bool aRepaint)
{
}
NS_IMETHODIMP
nsHTMLButtonControlFrame::HandleEvent(nsPresContext* aPresContext,
nsGUIEvent* aEvent,
nsEventStatus* aEventStatus)
{
// if disabled do nothing
if (mRenderer.isDisabled()) {
return NS_OK;
}
// mouse clicks are handled by content
// we don't want our children to get any events. So just pass it to frame.
return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
}
NS_IMETHODIMP
nsHTMLButtonControlFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
const nsRect& aDirtyRect,
const nsDisplayListSet& aLists)
{
nsDisplayList onTop;
if (IsVisibleForPainting(aBuilder)) {
nsresult rv = mRenderer.DisplayButton(aBuilder, aLists.BorderBackground(), &onTop);
NS_ENSURE_SUCCESS(rv, rv);
}
nsDisplayListCollection set;
// Do not allow the child subtree to receive events.
if (!aBuilder->IsForEventDelivery()) {
nsresult rv =
BuildDisplayListForChild(aBuilder, mFrames.FirstChild(), aDirtyRect, set,
DISPLAY_CHILD_FORCE_PSEUDO_STACKING_CONTEXT);
NS_ENSURE_SUCCESS(rv, rv);
// That should put the display items in set.Content()
}
// Put the foreground outline and focus rects on top of the children
set.Content()->AppendToTop(&onTop);
// clips to our padding box for <input>s but not <button>s, unless
// they have non-visible overflow..
if (IsInput() || GetStyleDisplay()->mOverflowX != NS_STYLE_OVERFLOW_VISIBLE) {
nsMargin border = GetStyleBorder()->GetComputedBorder();
nsRect rect(aBuilder->ToReferenceFrame(this), GetSize());
rect.Deflate(border);
nscoord radii[8];
GetPaddingBoxBorderRadii(radii);
nsresult rv = OverflowClip(aBuilder, set, aLists, rect, radii);
NS_ENSURE_SUCCESS(rv, rv);
} else {
set.MoveTo(aLists);
}
nsresult rv = DisplayOutline(aBuilder, aLists);
NS_ENSURE_SUCCESS(rv, rv);
// to draw border when selected in editor
return DisplaySelectionOverlay(aBuilder, aLists.Content());
}
nscoord
nsHTMLButtonControlFrame::GetMinWidth(nsRenderingContext* aRenderingContext)
{
nscoord result;
DISPLAY_MIN_WIDTH(this, result);
nsIFrame* kid = mFrames.FirstChild();
result = nsLayoutUtils::IntrinsicForContainer(aRenderingContext,
kid,
nsLayoutUtils::MIN_WIDTH);
result += mRenderer.GetAddedButtonBorderAndPadding().LeftRight();
return result;
}
nscoord
nsHTMLButtonControlFrame::GetPrefWidth(nsRenderingContext* aRenderingContext)
{
nscoord result;
DISPLAY_PREF_WIDTH(this, result);
nsIFrame* kid = mFrames.FirstChild();
result = nsLayoutUtils::IntrinsicForContainer(aRenderingContext,
kid,
nsLayoutUtils::PREF_WIDTH);
result += mRenderer.GetAddedButtonBorderAndPadding().LeftRight();
return result;
}
NS_IMETHODIMP
nsHTMLButtonControlFrame::Reflow(nsPresContext* aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
const nsHTMLReflowState& aReflowState,
nsReflowStatus& aStatus)
{
DO_GLOBAL_REFLOW_COUNT("nsHTMLButtonControlFrame");
DISPLAY_REFLOW(aPresContext, this, aReflowState, aDesiredSize, aStatus);
NS_PRECONDITION(aReflowState.ComputedWidth() != NS_INTRINSICSIZE,
"Should have real computed width by now");
if (mState & NS_FRAME_FIRST_REFLOW) {
nsFormControlFrame::RegUnRegAccessKey(static_cast<nsIFrame*>(this), true);
}
// Reflow the child
nsIFrame* firstKid = mFrames.FirstChild();
// XXXbz Eventually we may want to check-and-bail if
// !aReflowState.ShouldReflowAllKids() &&
// !NS_SUBTREE_DIRTY(firstKid).
// We'd need to cache our ascent for that, of course.
nsMargin focusPadding = mRenderer.GetAddedButtonBorderAndPadding();
// Reflow the contents of the button.
ReflowButtonContents(aPresContext, aDesiredSize, aReflowState, firstKid,
focusPadding, aStatus);
aDesiredSize.width = aReflowState.ComputedWidth();
// If computed use the computed value.
if (aReflowState.ComputedHeight() != NS_INTRINSICSIZE) {
aDesiredSize.height = aReflowState.ComputedHeight();
} else {
aDesiredSize.height += focusPadding.TopBottom();
// Make sure we obey min/max-height in the case when we're doing intrinsic
// sizing (we get it for free when we have a non-intrinsic
// aReflowState.ComputedHeight()). Note that we do this before adjusting
// for borderpadding, since mComputedMaxHeight and mComputedMinHeight are
// content heights.
aDesiredSize.height = NS_CSS_MINMAX(aDesiredSize.height,
aReflowState.mComputedMinHeight,
aReflowState.mComputedMaxHeight);
}
aDesiredSize.width += aReflowState.mComputedBorderPadding.LeftRight();
aDesiredSize.height += aReflowState.mComputedBorderPadding.TopBottom();
aDesiredSize.ascent +=
aReflowState.mComputedBorderPadding.top + focusPadding.top;
aDesiredSize.SetOverflowAreasToDesiredBounds();
ConsiderChildOverflow(aDesiredSize.mOverflowAreas, firstKid);
FinishReflowWithAbsoluteFrames(aPresContext, aDesiredSize, aReflowState, aStatus);
aStatus = NS_FRAME_COMPLETE;
NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize);
return NS_OK;
}
void
nsHTMLButtonControlFrame::ReflowButtonContents(nsPresContext* aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
const nsHTMLReflowState& aReflowState,
nsIFrame* aFirstKid,
nsMargin aFocusPadding,
nsReflowStatus& aStatus)
{
nsSize availSize(aReflowState.ComputedWidth(), NS_INTRINSICSIZE);
// Indent the child inside us by the focus border. We must do this separate
// from the regular border.
availSize.width -= aFocusPadding.LeftRight();
// See whether out availSize's width is big enough. If it's smaller than our
// intrinsic min width, that means that the kid wouldn't really fit; for a
// better look in such cases we adjust the available width and our left
// offset to allow the kid to spill left into our padding.
nscoord xoffset = aFocusPadding.left + aReflowState.mComputedBorderPadding.left;
nscoord extrawidth = GetMinWidth(aReflowState.rendContext) -
aReflowState.ComputedWidth();
if (extrawidth > 0) {
nscoord extraleft = extrawidth / 2;
nscoord extraright = extrawidth - extraleft;
NS_ASSERTION(extraright >=0, "How'd that happen?");
// Do not allow the extras to be bigger than the relevant padding
extraleft = NS_MIN(extraleft, aReflowState.mComputedPadding.left);
extraright = NS_MIN(extraright, aReflowState.mComputedPadding.right);
xoffset -= extraleft;
availSize.width += extraleft + extraright;
}
availSize.width = NS_MAX(availSize.width,0);
nsHTMLReflowState reflowState(aPresContext, aReflowState, aFirstKid,
availSize);
ReflowChild(aFirstKid, aPresContext, aDesiredSize, reflowState,
xoffset,
aFocusPadding.top + aReflowState.mComputedBorderPadding.top,
0, aStatus);
// calculate the min internal height so the contents gets centered correctly.
// XXXbz this assumes border-box sizing.
nscoord minInternalHeight = aReflowState.mComputedMinHeight -
aReflowState.mComputedBorderPadding.TopBottom();
minInternalHeight = NS_MAX(minInternalHeight, 0);
// center child vertically
nscoord yoff = 0;
if (aReflowState.ComputedHeight() != NS_INTRINSICSIZE) {
yoff = (aReflowState.ComputedHeight() - aDesiredSize.height)/2;
if (yoff < 0) {
yoff = 0;
}
} else if (aDesiredSize.height < minInternalHeight) {
yoff = (minInternalHeight - aDesiredSize.height) / 2;
}
// Place the child
FinishReflowChild(aFirstKid, aPresContext, &reflowState, aDesiredSize,
xoffset,
yoff + aFocusPadding.top + aReflowState.mComputedBorderPadding.top, 0);
if (aDesiredSize.ascent == nsHTMLReflowMetrics::ASK_FOR_BASELINE)
aDesiredSize.ascent = aFirstKid->GetBaseline();
// Adjust the baseline by our offset (since we moved the child's
// baseline by that much).
aDesiredSize.ascent += yoff;
}
int
nsHTMLButtonControlFrame::GetSkipSides() const
{
return 0;
}
nsresult nsHTMLButtonControlFrame::SetFormProperty(nsIAtom* aName, const nsAString& aValue)
{
if (nsGkAtoms::value == aName) {
return mContent->SetAttr(kNameSpaceID_None, nsGkAtoms::value,
aValue, true);
}
return NS_OK;
}
nsresult nsHTMLButtonControlFrame::GetFormProperty(nsIAtom* aName, nsAString& aValue) const
{
if (nsGkAtoms::value == aName)
mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::value, aValue);
return NS_OK;
}
nsStyleContext*
nsHTMLButtonControlFrame::GetAdditionalStyleContext(int32_t aIndex) const
{
return mRenderer.GetStyleContext(aIndex);
}
void
nsHTMLButtonControlFrame::SetAdditionalStyleContext(int32_t aIndex,
nsStyleContext* aStyleContext)
{
mRenderer.SetStyleContext(aIndex, aStyleContext);
}
NS_IMETHODIMP
nsHTMLButtonControlFrame::AppendFrames(ChildListID aListID,
nsFrameList& aFrameList)
{
NS_NOTREACHED("unsupported operation");
return NS_ERROR_UNEXPECTED;
}
NS_IMETHODIMP
nsHTMLButtonControlFrame::InsertFrames(ChildListID aListID,
nsIFrame* aPrevFrame,
nsFrameList& aFrameList)
{
NS_NOTREACHED("unsupported operation");
return NS_ERROR_UNEXPECTED;
}
NS_IMETHODIMP
nsHTMLButtonControlFrame::RemoveFrame(ChildListID aListID,
nsIFrame* aOldFrame)
{
NS_NOTREACHED("unsupported operation");
return NS_ERROR_UNEXPECTED;
}