layout/forms/nsHTMLButtonControlFrame.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsHTMLButtonControlFrame.h"
michael@0 7
michael@0 8 #include "nsContainerFrame.h"
michael@0 9 #include "nsIFormControlFrame.h"
michael@0 10 #include "nsPresContext.h"
michael@0 11 #include "nsGkAtoms.h"
michael@0 12 #include "nsButtonFrameRenderer.h"
michael@0 13 #include "nsCSSAnonBoxes.h"
michael@0 14 #include "nsFormControlFrame.h"
michael@0 15 #include "nsNameSpaceManager.h"
michael@0 16 #include "nsStyleSet.h"
michael@0 17 #include "nsDisplayList.h"
michael@0 18 #include <algorithm>
michael@0 19
michael@0 20 using namespace mozilla;
michael@0 21
michael@0 22 nsIFrame*
michael@0 23 NS_NewHTMLButtonControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
michael@0 24 {
michael@0 25 return new (aPresShell) nsHTMLButtonControlFrame(aContext);
michael@0 26 }
michael@0 27
michael@0 28 NS_IMPL_FRAMEARENA_HELPERS(nsHTMLButtonControlFrame)
michael@0 29
michael@0 30 nsHTMLButtonControlFrame::nsHTMLButtonControlFrame(nsStyleContext* aContext)
michael@0 31 : nsContainerFrame(aContext)
michael@0 32 {
michael@0 33 }
michael@0 34
michael@0 35 nsHTMLButtonControlFrame::~nsHTMLButtonControlFrame()
michael@0 36 {
michael@0 37 }
michael@0 38
michael@0 39 void
michael@0 40 nsHTMLButtonControlFrame::DestroyFrom(nsIFrame* aDestructRoot)
michael@0 41 {
michael@0 42 nsFormControlFrame::RegUnRegAccessKey(static_cast<nsIFrame*>(this), false);
michael@0 43 nsContainerFrame::DestroyFrom(aDestructRoot);
michael@0 44 }
michael@0 45
michael@0 46 void
michael@0 47 nsHTMLButtonControlFrame::Init(
michael@0 48 nsIContent* aContent,
michael@0 49 nsIFrame* aParent,
michael@0 50 nsIFrame* aPrevInFlow)
michael@0 51 {
michael@0 52 nsContainerFrame::Init(aContent, aParent, aPrevInFlow);
michael@0 53 mRenderer.SetFrame(this, PresContext());
michael@0 54 }
michael@0 55
michael@0 56 NS_QUERYFRAME_HEAD(nsHTMLButtonControlFrame)
michael@0 57 NS_QUERYFRAME_ENTRY(nsIFormControlFrame)
michael@0 58 NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
michael@0 59
michael@0 60 #ifdef ACCESSIBILITY
michael@0 61 a11y::AccType
michael@0 62 nsHTMLButtonControlFrame::AccessibleType()
michael@0 63 {
michael@0 64 return a11y::eHTMLButtonType;
michael@0 65 }
michael@0 66 #endif
michael@0 67
michael@0 68 nsIAtom*
michael@0 69 nsHTMLButtonControlFrame::GetType() const
michael@0 70 {
michael@0 71 return nsGkAtoms::HTMLButtonControlFrame;
michael@0 72 }
michael@0 73
michael@0 74 void
michael@0 75 nsHTMLButtonControlFrame::SetFocus(bool aOn, bool aRepaint)
michael@0 76 {
michael@0 77 }
michael@0 78
michael@0 79 nsresult
michael@0 80 nsHTMLButtonControlFrame::HandleEvent(nsPresContext* aPresContext,
michael@0 81 WidgetGUIEvent* aEvent,
michael@0 82 nsEventStatus* aEventStatus)
michael@0 83 {
michael@0 84 // if disabled do nothing
michael@0 85 if (mRenderer.isDisabled()) {
michael@0 86 return NS_OK;
michael@0 87 }
michael@0 88
michael@0 89 // mouse clicks are handled by content
michael@0 90 // we don't want our children to get any events. So just pass it to frame.
michael@0 91 return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
michael@0 92 }
michael@0 93
michael@0 94
michael@0 95 void
michael@0 96 nsHTMLButtonControlFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
michael@0 97 const nsRect& aDirtyRect,
michael@0 98 const nsDisplayListSet& aLists)
michael@0 99 {
michael@0 100 nsDisplayList onTop;
michael@0 101 if (IsVisibleForPainting(aBuilder)) {
michael@0 102 mRenderer.DisplayButton(aBuilder, aLists.BorderBackground(), &onTop);
michael@0 103 }
michael@0 104
michael@0 105 nsDisplayListCollection set;
michael@0 106
michael@0 107 // Do not allow the child subtree to receive events.
michael@0 108 if (!aBuilder->IsForEventDelivery()) {
michael@0 109 DisplayListClipState::AutoSaveRestore clipState(aBuilder);
michael@0 110
michael@0 111 if (IsInput() || StyleDisplay()->mOverflowX != NS_STYLE_OVERFLOW_VISIBLE) {
michael@0 112 nsMargin border = StyleBorder()->GetComputedBorder();
michael@0 113 nsRect rect(aBuilder->ToReferenceFrame(this), GetSize());
michael@0 114 rect.Deflate(border);
michael@0 115 nscoord radii[8];
michael@0 116 bool hasRadii = GetPaddingBoxBorderRadii(radii);
michael@0 117 clipState.ClipContainingBlockDescendants(rect, hasRadii ? radii : nullptr);
michael@0 118 }
michael@0 119
michael@0 120 BuildDisplayListForChild(aBuilder, mFrames.FirstChild(), aDirtyRect, set,
michael@0 121 DISPLAY_CHILD_FORCE_PSEUDO_STACKING_CONTEXT);
michael@0 122 // That should put the display items in set.Content()
michael@0 123 }
michael@0 124
michael@0 125 // Put the foreground outline and focus rects on top of the children
michael@0 126 set.Content()->AppendToTop(&onTop);
michael@0 127 set.MoveTo(aLists);
michael@0 128
michael@0 129 DisplayOutline(aBuilder, aLists);
michael@0 130
michael@0 131 // to draw border when selected in editor
michael@0 132 DisplaySelectionOverlay(aBuilder, aLists.Content());
michael@0 133 }
michael@0 134
michael@0 135 nscoord
michael@0 136 nsHTMLButtonControlFrame::GetMinWidth(nsRenderingContext* aRenderingContext)
michael@0 137 {
michael@0 138 nscoord result;
michael@0 139 DISPLAY_MIN_WIDTH(this, result);
michael@0 140
michael@0 141 nsIFrame* kid = mFrames.FirstChild();
michael@0 142 result = nsLayoutUtils::IntrinsicForContainer(aRenderingContext,
michael@0 143 kid,
michael@0 144 nsLayoutUtils::MIN_WIDTH);
michael@0 145
michael@0 146 result += mRenderer.GetAddedButtonBorderAndPadding().LeftRight();
michael@0 147
michael@0 148 return result;
michael@0 149 }
michael@0 150
michael@0 151 nscoord
michael@0 152 nsHTMLButtonControlFrame::GetPrefWidth(nsRenderingContext* aRenderingContext)
michael@0 153 {
michael@0 154 nscoord result;
michael@0 155 DISPLAY_PREF_WIDTH(this, result);
michael@0 156
michael@0 157 nsIFrame* kid = mFrames.FirstChild();
michael@0 158 result = nsLayoutUtils::IntrinsicForContainer(aRenderingContext,
michael@0 159 kid,
michael@0 160 nsLayoutUtils::PREF_WIDTH);
michael@0 161 result += mRenderer.GetAddedButtonBorderAndPadding().LeftRight();
michael@0 162 return result;
michael@0 163 }
michael@0 164
michael@0 165 nsresult
michael@0 166 nsHTMLButtonControlFrame::Reflow(nsPresContext* aPresContext,
michael@0 167 nsHTMLReflowMetrics& aDesiredSize,
michael@0 168 const nsHTMLReflowState& aReflowState,
michael@0 169 nsReflowStatus& aStatus)
michael@0 170 {
michael@0 171 DO_GLOBAL_REFLOW_COUNT("nsHTMLButtonControlFrame");
michael@0 172 DISPLAY_REFLOW(aPresContext, this, aReflowState, aDesiredSize, aStatus);
michael@0 173
michael@0 174 NS_PRECONDITION(aReflowState.ComputedWidth() != NS_INTRINSICSIZE,
michael@0 175 "Should have real computed width by now");
michael@0 176
michael@0 177 if (mState & NS_FRAME_FIRST_REFLOW) {
michael@0 178 nsFormControlFrame::RegUnRegAccessKey(static_cast<nsIFrame*>(this), true);
michael@0 179 }
michael@0 180
michael@0 181 // Reflow the child
michael@0 182 nsIFrame* firstKid = mFrames.FirstChild();
michael@0 183
michael@0 184 MOZ_ASSERT(firstKid, "Button should have a child frame for its contents");
michael@0 185 MOZ_ASSERT(!firstKid->GetNextSibling(),
michael@0 186 "Button should have exactly one child frame");
michael@0 187 MOZ_ASSERT(firstKid->StyleContext()->GetPseudo() ==
michael@0 188 nsCSSAnonBoxes::buttonContent,
michael@0 189 "Button's child frame has unexpected pseudo type!");
michael@0 190
michael@0 191 // XXXbz Eventually we may want to check-and-bail if
michael@0 192 // !aReflowState.ShouldReflowAllKids() &&
michael@0 193 // !NS_SUBTREE_DIRTY(firstKid).
michael@0 194 // We'd need to cache our ascent for that, of course.
michael@0 195
michael@0 196 // Reflow the contents of the button.
michael@0 197 // (This populates our aDesiredSize, too.)
michael@0 198 ReflowButtonContents(aPresContext, aDesiredSize,
michael@0 199 aReflowState, firstKid);
michael@0 200
michael@0 201 ConsiderChildOverflow(aDesiredSize.mOverflowAreas, firstKid);
michael@0 202
michael@0 203 aStatus = NS_FRAME_COMPLETE;
michael@0 204 FinishReflowWithAbsoluteFrames(aPresContext, aDesiredSize,
michael@0 205 aReflowState, aStatus);
michael@0 206
michael@0 207 NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize);
michael@0 208 return NS_OK;
michael@0 209 }
michael@0 210
michael@0 211 // Helper-function that lets us clone the button's reflow state, but with its
michael@0 212 // ComputedWidth and ComputedHeight reduced by the amount of renderer-specific
michael@0 213 // focus border and padding that we're using. (This lets us provide a more
michael@0 214 // appropriate content-box size for descendents' percent sizes to resolve
michael@0 215 // against.)
michael@0 216 static nsHTMLReflowState
michael@0 217 CloneReflowStateWithReducedContentBox(
michael@0 218 const nsHTMLReflowState& aButtonReflowState,
michael@0 219 const nsMargin& aFocusPadding)
michael@0 220 {
michael@0 221 nscoord adjustedWidth =
michael@0 222 aButtonReflowState.ComputedWidth() - aFocusPadding.LeftRight();
michael@0 223 adjustedWidth = std::max(0, adjustedWidth);
michael@0 224
michael@0 225 // (Only adjust height if it's an actual length.)
michael@0 226 nscoord adjustedHeight = aButtonReflowState.ComputedHeight();
michael@0 227 if (adjustedHeight != NS_INTRINSICSIZE) {
michael@0 228 adjustedHeight -= aFocusPadding.TopBottom();
michael@0 229 adjustedHeight = std::max(0, adjustedHeight);
michael@0 230 }
michael@0 231
michael@0 232 nsHTMLReflowState clone(aButtonReflowState);
michael@0 233 clone.SetComputedWidth(adjustedWidth);
michael@0 234 clone.SetComputedHeight(adjustedHeight);
michael@0 235
michael@0 236 return clone;
michael@0 237 }
michael@0 238
michael@0 239 void
michael@0 240 nsHTMLButtonControlFrame::ReflowButtonContents(nsPresContext* aPresContext,
michael@0 241 nsHTMLReflowMetrics& aButtonDesiredSize,
michael@0 242 const nsHTMLReflowState& aButtonReflowState,
michael@0 243 nsIFrame* aFirstKid)
michael@0 244 {
michael@0 245 // Buttons have some bonus renderer-determined border/padding,
michael@0 246 // which occupies part of the button's content-box area:
michael@0 247 const nsMargin focusPadding = mRenderer.GetAddedButtonBorderAndPadding();
michael@0 248
michael@0 249 nsSize availSize(aButtonReflowState.ComputedWidth(), NS_INTRINSICSIZE);
michael@0 250
michael@0 251 // Indent the child inside us by the focus border. We must do this separate
michael@0 252 // from the regular border.
michael@0 253 availSize.width -= focusPadding.LeftRight();
michael@0 254
michael@0 255 // See whether out availSize's width is big enough. If it's smaller than our
michael@0 256 // intrinsic min width, that means that the kid wouldn't really fit; for a
michael@0 257 // better look in such cases we adjust the available width and our left
michael@0 258 // offset to allow the kid to spill left into our padding.
michael@0 259 nscoord xoffset = focusPadding.left +
michael@0 260 aButtonReflowState.ComputedPhysicalBorderPadding().left;
michael@0 261 nscoord extrawidth = GetMinWidth(aButtonReflowState.rendContext) -
michael@0 262 aButtonReflowState.ComputedWidth();
michael@0 263 if (extrawidth > 0) {
michael@0 264 nscoord extraleft = extrawidth / 2;
michael@0 265 nscoord extraright = extrawidth - extraleft;
michael@0 266 NS_ASSERTION(extraright >=0, "How'd that happen?");
michael@0 267
michael@0 268 // Do not allow the extras to be bigger than the relevant padding
michael@0 269 extraleft = std::min(extraleft, aButtonReflowState.ComputedPhysicalPadding().left);
michael@0 270 extraright = std::min(extraright, aButtonReflowState.ComputedPhysicalPadding().right);
michael@0 271 xoffset -= extraleft;
michael@0 272 availSize.width += extraleft + extraright;
michael@0 273 }
michael@0 274 availSize.width = std::max(availSize.width,0);
michael@0 275
michael@0 276 // Give child a clone of the button's reflow state, with height/width reduced
michael@0 277 // by focusPadding, so that descendants with height:100% don't protrude.
michael@0 278 nsHTMLReflowState adjustedButtonReflowState =
michael@0 279 CloneReflowStateWithReducedContentBox(aButtonReflowState, focusPadding);
michael@0 280
michael@0 281 nsHTMLReflowState contentsReflowState(aPresContext,
michael@0 282 adjustedButtonReflowState,
michael@0 283 aFirstKid, availSize);
michael@0 284
michael@0 285 nsReflowStatus contentsReflowStatus;
michael@0 286 nsHTMLReflowMetrics contentsDesiredSize(aButtonReflowState);
michael@0 287 ReflowChild(aFirstKid, aPresContext,
michael@0 288 contentsDesiredSize, contentsReflowState,
michael@0 289 xoffset,
michael@0 290 focusPadding.top + aButtonReflowState.ComputedPhysicalBorderPadding().top,
michael@0 291 0, contentsReflowStatus);
michael@0 292 MOZ_ASSERT(NS_FRAME_IS_COMPLETE(contentsReflowStatus),
michael@0 293 "We gave button-contents frame unconstrained available height, "
michael@0 294 "so it should be complete");
michael@0 295
michael@0 296 // Compute the button's content-box height:
michael@0 297 nscoord buttonContentBoxHeight = 0;
michael@0 298 if (aButtonReflowState.ComputedHeight() != NS_INTRINSICSIZE) {
michael@0 299 // Button has a fixed height -- that's its content-box height.
michael@0 300 buttonContentBoxHeight = aButtonReflowState.ComputedHeight();
michael@0 301 } else {
michael@0 302 // Button is intrinsically sized -- it should shrinkwrap the
michael@0 303 // button-contents' height, plus any focus-padding space:
michael@0 304 buttonContentBoxHeight =
michael@0 305 contentsDesiredSize.Height() + focusPadding.TopBottom();
michael@0 306
michael@0 307 // Make sure we obey min/max-height in the case when we're doing intrinsic
michael@0 308 // sizing (we get it for free when we have a non-intrinsic
michael@0 309 // aButtonReflowState.ComputedHeight()). Note that we do this before
michael@0 310 // adjusting for borderpadding, since mComputedMaxHeight and
michael@0 311 // mComputedMinHeight are content heights.
michael@0 312 buttonContentBoxHeight =
michael@0 313 NS_CSS_MINMAX(buttonContentBoxHeight,
michael@0 314 aButtonReflowState.ComputedMinHeight(),
michael@0 315 aButtonReflowState.ComputedMaxHeight());
michael@0 316 }
michael@0 317
michael@0 318 // Center child vertically in the button
michael@0 319 // (technically, inside of the button's focus-padding area)
michael@0 320 nscoord extraSpace =
michael@0 321 buttonContentBoxHeight - focusPadding.TopBottom() -
michael@0 322 contentsDesiredSize.Height();
michael@0 323
michael@0 324 nscoord yoffset = std::max(0, extraSpace / 2);
michael@0 325
michael@0 326 // Adjust yoffset to be in terms of the button's frame-rect, instead of
michael@0 327 // its focus-padding rect:
michael@0 328 yoffset += focusPadding.top + aButtonReflowState.ComputedPhysicalBorderPadding().top;
michael@0 329
michael@0 330 // Place the child
michael@0 331 FinishReflowChild(aFirstKid, aPresContext,
michael@0 332 contentsDesiredSize, &contentsReflowState,
michael@0 333 xoffset, yoffset, 0);
michael@0 334
michael@0 335 // Make sure we have a useful 'ascent' value for the child
michael@0 336 if (contentsDesiredSize.TopAscent() == nsHTMLReflowMetrics::ASK_FOR_BASELINE) {
michael@0 337 contentsDesiredSize.SetTopAscent(aFirstKid->GetBaseline());
michael@0 338 }
michael@0 339
michael@0 340 // OK, we're done with the child frame.
michael@0 341 // Use what we learned to populate the button frame's reflow metrics.
michael@0 342 // * Button's height & width are content-box size + border-box contribution:
michael@0 343 aButtonDesiredSize.Width() = aButtonReflowState.ComputedWidth() +
michael@0 344 aButtonReflowState.ComputedPhysicalBorderPadding().LeftRight();
michael@0 345
michael@0 346 aButtonDesiredSize.Height() = buttonContentBoxHeight +
michael@0 347 aButtonReflowState.ComputedPhysicalBorderPadding().TopBottom();
michael@0 348
michael@0 349 // * Button's ascent is its child's ascent, plus the child's y-offset
michael@0 350 // within our frame:
michael@0 351 aButtonDesiredSize.SetTopAscent(contentsDesiredSize.TopAscent() + yoffset);
michael@0 352
michael@0 353 aButtonDesiredSize.SetOverflowAreasToDesiredBounds();
michael@0 354 }
michael@0 355
michael@0 356 nsresult nsHTMLButtonControlFrame::SetFormProperty(nsIAtom* aName, const nsAString& aValue)
michael@0 357 {
michael@0 358 if (nsGkAtoms::value == aName) {
michael@0 359 return mContent->SetAttr(kNameSpaceID_None, nsGkAtoms::value,
michael@0 360 aValue, true);
michael@0 361 }
michael@0 362 return NS_OK;
michael@0 363 }
michael@0 364
michael@0 365 nsStyleContext*
michael@0 366 nsHTMLButtonControlFrame::GetAdditionalStyleContext(int32_t aIndex) const
michael@0 367 {
michael@0 368 return mRenderer.GetStyleContext(aIndex);
michael@0 369 }
michael@0 370
michael@0 371 void
michael@0 372 nsHTMLButtonControlFrame::SetAdditionalStyleContext(int32_t aIndex,
michael@0 373 nsStyleContext* aStyleContext)
michael@0 374 {
michael@0 375 mRenderer.SetStyleContext(aIndex, aStyleContext);
michael@0 376 }
michael@0 377
michael@0 378 nsresult
michael@0 379 nsHTMLButtonControlFrame::AppendFrames(ChildListID aListID,
michael@0 380 nsFrameList& aFrameList)
michael@0 381 {
michael@0 382 NS_NOTREACHED("unsupported operation");
michael@0 383 return NS_ERROR_UNEXPECTED;
michael@0 384 }
michael@0 385
michael@0 386 nsresult
michael@0 387 nsHTMLButtonControlFrame::InsertFrames(ChildListID aListID,
michael@0 388 nsIFrame* aPrevFrame,
michael@0 389 nsFrameList& aFrameList)
michael@0 390 {
michael@0 391 NS_NOTREACHED("unsupported operation");
michael@0 392 return NS_ERROR_UNEXPECTED;
michael@0 393 }
michael@0 394
michael@0 395 nsresult
michael@0 396 nsHTMLButtonControlFrame::RemoveFrame(ChildListID aListID,
michael@0 397 nsIFrame* aOldFrame)
michael@0 398 {
michael@0 399 NS_NOTREACHED("unsupported operation");
michael@0 400 return NS_ERROR_UNEXPECTED;
michael@0 401 }

mercurial