layout/forms/nsButtonFrameRenderer.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 #include "nsButtonFrameRenderer.h"
michael@0 6 #include "nsCSSRendering.h"
michael@0 7 #include "nsPresContext.h"
michael@0 8 #include "nsGkAtoms.h"
michael@0 9 #include "nsCSSPseudoElements.h"
michael@0 10 #include "nsNameSpaceManager.h"
michael@0 11 #include "nsStyleSet.h"
michael@0 12 #include "nsDisplayList.h"
michael@0 13 #include "nsITheme.h"
michael@0 14 #include "nsFrame.h"
michael@0 15 #include "mozilla/EventStates.h"
michael@0 16 #include "mozilla/dom/Element.h"
michael@0 17
michael@0 18 #define ACTIVE "active"
michael@0 19 #define HOVER "hover"
michael@0 20 #define FOCUS "focus"
michael@0 21
michael@0 22 nsButtonFrameRenderer::nsButtonFrameRenderer()
michael@0 23 {
michael@0 24 MOZ_COUNT_CTOR(nsButtonFrameRenderer);
michael@0 25 }
michael@0 26
michael@0 27 nsButtonFrameRenderer::~nsButtonFrameRenderer()
michael@0 28 {
michael@0 29 MOZ_COUNT_DTOR(nsButtonFrameRenderer);
michael@0 30 }
michael@0 31
michael@0 32 void
michael@0 33 nsButtonFrameRenderer::SetFrame(nsFrame* aFrame, nsPresContext* aPresContext)
michael@0 34 {
michael@0 35 mFrame = aFrame;
michael@0 36 ReResolveStyles(aPresContext);
michael@0 37 }
michael@0 38
michael@0 39 nsIFrame*
michael@0 40 nsButtonFrameRenderer::GetFrame()
michael@0 41 {
michael@0 42 return mFrame;
michael@0 43 }
michael@0 44
michael@0 45 void
michael@0 46 nsButtonFrameRenderer::SetDisabled(bool aDisabled, bool notify)
michael@0 47 {
michael@0 48 if (aDisabled)
michael@0 49 mFrame->GetContent()->SetAttr(kNameSpaceID_None, nsGkAtoms::disabled, EmptyString(),
michael@0 50 notify);
michael@0 51 else
michael@0 52 mFrame->GetContent()->UnsetAttr(kNameSpaceID_None, nsGkAtoms::disabled, notify);
michael@0 53 }
michael@0 54
michael@0 55 bool
michael@0 56 nsButtonFrameRenderer::isDisabled()
michael@0 57 {
michael@0 58 return mFrame->GetContent()->AsElement()->
michael@0 59 State().HasState(NS_EVENT_STATE_DISABLED);
michael@0 60 }
michael@0 61
michael@0 62 class nsDisplayButtonBoxShadowOuter : public nsDisplayItem {
michael@0 63 public:
michael@0 64 nsDisplayButtonBoxShadowOuter(nsDisplayListBuilder* aBuilder,
michael@0 65 nsButtonFrameRenderer* aRenderer)
michael@0 66 : nsDisplayItem(aBuilder, aRenderer->GetFrame()), mBFR(aRenderer) {
michael@0 67 MOZ_COUNT_CTOR(nsDisplayButtonBoxShadowOuter);
michael@0 68 }
michael@0 69 #ifdef NS_BUILD_REFCNT_LOGGING
michael@0 70 virtual ~nsDisplayButtonBoxShadowOuter() {
michael@0 71 MOZ_COUNT_DTOR(nsDisplayButtonBoxShadowOuter);
michael@0 72 }
michael@0 73 #endif
michael@0 74
michael@0 75 virtual void Paint(nsDisplayListBuilder* aBuilder,
michael@0 76 nsRenderingContext* aCtx) MOZ_OVERRIDE;
michael@0 77 virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder,
michael@0 78 bool* aSnap) MOZ_OVERRIDE;
michael@0 79 NS_DISPLAY_DECL_NAME("ButtonBoxShadowOuter", TYPE_BUTTON_BOX_SHADOW_OUTER)
michael@0 80 private:
michael@0 81 nsButtonFrameRenderer* mBFR;
michael@0 82 };
michael@0 83
michael@0 84 nsRect
michael@0 85 nsDisplayButtonBoxShadowOuter::GetBounds(nsDisplayListBuilder* aBuilder, bool* aSnap) {
michael@0 86 *aSnap = false;
michael@0 87 return mFrame->GetVisualOverflowRectRelativeToSelf() + ToReferenceFrame();
michael@0 88 }
michael@0 89
michael@0 90 void
michael@0 91 nsDisplayButtonBoxShadowOuter::Paint(nsDisplayListBuilder* aBuilder,
michael@0 92 nsRenderingContext* aCtx) {
michael@0 93 nsRect frameRect = nsRect(ToReferenceFrame(), mFrame->GetSize());
michael@0 94
michael@0 95 nsRect buttonRect;
michael@0 96 mBFR->GetButtonRect(frameRect, buttonRect);
michael@0 97
michael@0 98 nsCSSRendering::PaintBoxShadowOuter(mFrame->PresContext(), *aCtx, mFrame,
michael@0 99 buttonRect, mVisibleRect);
michael@0 100 }
michael@0 101
michael@0 102 class nsDisplayButtonBorderBackground : public nsDisplayItem {
michael@0 103 public:
michael@0 104 nsDisplayButtonBorderBackground(nsDisplayListBuilder* aBuilder,
michael@0 105 nsButtonFrameRenderer* aRenderer)
michael@0 106 : nsDisplayItem(aBuilder, aRenderer->GetFrame()), mBFR(aRenderer) {
michael@0 107 MOZ_COUNT_CTOR(nsDisplayButtonBorderBackground);
michael@0 108 }
michael@0 109 #ifdef NS_BUILD_REFCNT_LOGGING
michael@0 110 virtual ~nsDisplayButtonBorderBackground() {
michael@0 111 MOZ_COUNT_DTOR(nsDisplayButtonBorderBackground);
michael@0 112 }
michael@0 113 #endif
michael@0 114
michael@0 115 virtual void HitTest(nsDisplayListBuilder* aBuilder, const nsRect& aRect,
michael@0 116 HitTestState* aState,
michael@0 117 nsTArray<nsIFrame*> *aOutFrames) MOZ_OVERRIDE {
michael@0 118 aOutFrames->AppendElement(mFrame);
michael@0 119 }
michael@0 120 virtual void Paint(nsDisplayListBuilder* aBuilder,
michael@0 121 nsRenderingContext* aCtx) MOZ_OVERRIDE;
michael@0 122 virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder,
michael@0 123 bool* aSnap) MOZ_OVERRIDE;
michael@0 124 virtual void ComputeInvalidationRegion(nsDisplayListBuilder* aBuilder,
michael@0 125 const nsDisplayItemGeometry* aGeometry,
michael@0 126 nsRegion *aInvalidRegion) MOZ_OVERRIDE;
michael@0 127 NS_DISPLAY_DECL_NAME("ButtonBorderBackground", TYPE_BUTTON_BORDER_BACKGROUND)
michael@0 128 private:
michael@0 129 nsButtonFrameRenderer* mBFR;
michael@0 130 };
michael@0 131
michael@0 132 nsRect
michael@0 133 nsDisplayButtonBorderBackground::GetBounds(nsDisplayListBuilder* aBuilder, bool* aSnap) {
michael@0 134 *aSnap = false;
michael@0 135 return aBuilder->IsForEventDelivery() ? nsRect(ToReferenceFrame(), mFrame->GetSize())
michael@0 136 : mFrame->GetVisualOverflowRectRelativeToSelf() + ToReferenceFrame();
michael@0 137 }
michael@0 138
michael@0 139 class nsDisplayButtonForeground : public nsDisplayItem {
michael@0 140 public:
michael@0 141 nsDisplayButtonForeground(nsDisplayListBuilder* aBuilder,
michael@0 142 nsButtonFrameRenderer* aRenderer)
michael@0 143 : nsDisplayItem(aBuilder, aRenderer->GetFrame()), mBFR(aRenderer) {
michael@0 144 MOZ_COUNT_CTOR(nsDisplayButtonForeground);
michael@0 145 }
michael@0 146 #ifdef NS_BUILD_REFCNT_LOGGING
michael@0 147 virtual ~nsDisplayButtonForeground() {
michael@0 148 MOZ_COUNT_DTOR(nsDisplayButtonForeground);
michael@0 149 }
michael@0 150 #endif
michael@0 151
michael@0 152 virtual void Paint(nsDisplayListBuilder* aBuilder,
michael@0 153 nsRenderingContext* aCtx) MOZ_OVERRIDE;
michael@0 154 NS_DISPLAY_DECL_NAME("ButtonForeground", TYPE_BUTTON_FOREGROUND)
michael@0 155 private:
michael@0 156 nsButtonFrameRenderer* mBFR;
michael@0 157 };
michael@0 158
michael@0 159 void
michael@0 160 nsDisplayButtonBorderBackground::ComputeInvalidationRegion(nsDisplayListBuilder* aBuilder,
michael@0 161 const nsDisplayItemGeometry* aGeometry,
michael@0 162 nsRegion *aInvalidRegion)
michael@0 163 {
michael@0 164 AddInvalidRegionForSyncDecodeBackgroundImages(aBuilder, aGeometry, aInvalidRegion);
michael@0 165
michael@0 166 nsDisplayItem::ComputeInvalidationRegion(aBuilder, aGeometry, aInvalidRegion);
michael@0 167 }
michael@0 168
michael@0 169 void nsDisplayButtonBorderBackground::Paint(nsDisplayListBuilder* aBuilder,
michael@0 170 nsRenderingContext* aCtx)
michael@0 171 {
michael@0 172 NS_ASSERTION(mFrame, "No frame?");
michael@0 173 nsPresContext* pc = mFrame->PresContext();
michael@0 174 nsRect r = nsRect(ToReferenceFrame(), mFrame->GetSize());
michael@0 175
michael@0 176 // draw the border and background inside the focus and outline borders
michael@0 177 mBFR->PaintBorderAndBackground(pc, *aCtx, mVisibleRect, r,
michael@0 178 aBuilder->GetBackgroundPaintFlags());
michael@0 179 }
michael@0 180
michael@0 181 void nsDisplayButtonForeground::Paint(nsDisplayListBuilder* aBuilder,
michael@0 182 nsRenderingContext* aCtx)
michael@0 183 {
michael@0 184 nsPresContext *presContext = mFrame->PresContext();
michael@0 185 const nsStyleDisplay *disp = mFrame->StyleDisplay();
michael@0 186 if (!mFrame->IsThemed(disp) ||
michael@0 187 !presContext->GetTheme()->ThemeDrawsFocusForWidget(disp->mAppearance)) {
michael@0 188 // draw the focus and outline borders
michael@0 189 nsRect r = nsRect(ToReferenceFrame(), mFrame->GetSize());
michael@0 190 mBFR->PaintOutlineAndFocusBorders(presContext, *aCtx, mVisibleRect, r);
michael@0 191 }
michael@0 192 }
michael@0 193
michael@0 194 nsresult
michael@0 195 nsButtonFrameRenderer::DisplayButton(nsDisplayListBuilder* aBuilder,
michael@0 196 nsDisplayList* aBackground,
michael@0 197 nsDisplayList* aForeground)
michael@0 198 {
michael@0 199 if (mFrame->StyleBorder()->mBoxShadow) {
michael@0 200 aBackground->AppendNewToTop(new (aBuilder)
michael@0 201 nsDisplayButtonBoxShadowOuter(aBuilder, this));
michael@0 202 }
michael@0 203
michael@0 204 // Almost all buttons draw some kind of background so there's not much
michael@0 205 // point in checking whether we should create this item.
michael@0 206 aBackground->AppendNewToTop(new (aBuilder)
michael@0 207 nsDisplayButtonBorderBackground(aBuilder, this));
michael@0 208
michael@0 209 // Only display focus rings if we actually have them. Since at most one
michael@0 210 // button would normally display a focus ring, most buttons won't have them.
michael@0 211 if ((mOuterFocusStyle && mOuterFocusStyle->StyleBorder()->HasBorder()) ||
michael@0 212 (mInnerFocusStyle && mInnerFocusStyle->StyleBorder()->HasBorder())) {
michael@0 213 aForeground->AppendNewToTop(new (aBuilder)
michael@0 214 nsDisplayButtonForeground(aBuilder, this));
michael@0 215 }
michael@0 216 return NS_OK;
michael@0 217 }
michael@0 218
michael@0 219 void
michael@0 220 nsButtonFrameRenderer::PaintOutlineAndFocusBorders(nsPresContext* aPresContext,
michael@0 221 nsRenderingContext& aRenderingContext,
michael@0 222 const nsRect& aDirtyRect,
michael@0 223 const nsRect& aRect)
michael@0 224 {
michael@0 225 // once we have all that we'll draw the focus if we have it. We will
michael@0 226 // need to draw 2 focuses, the inner and the outer. This is so we
michael@0 227 // can do any kind of look and feel. Some buttons have focus on the
michael@0 228 // outside like mac and motif. While others like windows have it
michael@0 229 // inside (dotted line). Usually only one will be specifed. But I
michael@0 230 // guess you could have both if you wanted to.
michael@0 231
michael@0 232 nsRect rect;
michael@0 233
michael@0 234 if (mOuterFocusStyle) {
michael@0 235 // ---------- paint the outer focus border -------------
michael@0 236
michael@0 237 GetButtonOuterFocusRect(aRect, rect);
michael@0 238
michael@0 239 nsCSSRendering::PaintBorder(aPresContext, aRenderingContext, mFrame,
michael@0 240 aDirtyRect, rect, mOuterFocusStyle);
michael@0 241 }
michael@0 242
michael@0 243 if (mInnerFocusStyle) {
michael@0 244 // ---------- paint the inner focus border -------------
michael@0 245
michael@0 246 GetButtonInnerFocusRect(aRect, rect);
michael@0 247
michael@0 248 nsCSSRendering::PaintBorder(aPresContext, aRenderingContext, mFrame,
michael@0 249 aDirtyRect, rect, mInnerFocusStyle);
michael@0 250 }
michael@0 251 }
michael@0 252
michael@0 253
michael@0 254 void
michael@0 255 nsButtonFrameRenderer::PaintBorderAndBackground(nsPresContext* aPresContext,
michael@0 256 nsRenderingContext& aRenderingContext,
michael@0 257 const nsRect& aDirtyRect,
michael@0 258 const nsRect& aRect,
michael@0 259 uint32_t aBGFlags)
michael@0 260
michael@0 261 {
michael@0 262 // get the button rect this is inside the focus and outline rects
michael@0 263 nsRect buttonRect;
michael@0 264 GetButtonRect(aRect, buttonRect);
michael@0 265
michael@0 266 nsStyleContext* context = mFrame->StyleContext();
michael@0 267
michael@0 268 nsCSSRendering::PaintBackground(aPresContext, aRenderingContext, mFrame,
michael@0 269 aDirtyRect, buttonRect, aBGFlags);
michael@0 270 nsCSSRendering::PaintBoxShadowInner(aPresContext, aRenderingContext,
michael@0 271 mFrame, buttonRect, aDirtyRect);
michael@0 272 nsCSSRendering::PaintBorder(aPresContext, aRenderingContext, mFrame,
michael@0 273 aDirtyRect, buttonRect, context);
michael@0 274 }
michael@0 275
michael@0 276
michael@0 277 void
michael@0 278 nsButtonFrameRenderer::GetButtonOuterFocusRect(const nsRect& aRect, nsRect& focusRect)
michael@0 279 {
michael@0 280 focusRect = aRect;
michael@0 281 }
michael@0 282
michael@0 283 void
michael@0 284 nsButtonFrameRenderer::GetButtonRect(const nsRect& aRect, nsRect& r)
michael@0 285 {
michael@0 286 r = aRect;
michael@0 287 r.Deflate(GetButtonOuterFocusBorderAndPadding());
michael@0 288 }
michael@0 289
michael@0 290
michael@0 291 void
michael@0 292 nsButtonFrameRenderer::GetButtonInnerFocusRect(const nsRect& aRect, nsRect& focusRect)
michael@0 293 {
michael@0 294 GetButtonRect(aRect, focusRect);
michael@0 295 focusRect.Deflate(GetButtonBorderAndPadding());
michael@0 296 focusRect.Deflate(GetButtonInnerFocusMargin());
michael@0 297 }
michael@0 298
michael@0 299
michael@0 300 nsMargin
michael@0 301 nsButtonFrameRenderer::GetButtonOuterFocusBorderAndPadding()
michael@0 302 {
michael@0 303 nsMargin result(0,0,0,0);
michael@0 304
michael@0 305 if (mOuterFocusStyle) {
michael@0 306 if (!mOuterFocusStyle->StylePadding()->GetPadding(result)) {
michael@0 307 NS_NOTYETIMPLEMENTED("percentage padding");
michael@0 308 }
michael@0 309 result += mOuterFocusStyle->StyleBorder()->GetComputedBorder();
michael@0 310 }
michael@0 311
michael@0 312 return result;
michael@0 313 }
michael@0 314
michael@0 315 nsMargin
michael@0 316 nsButtonFrameRenderer::GetButtonBorderAndPadding()
michael@0 317 {
michael@0 318 return mFrame->GetUsedBorderAndPadding();
michael@0 319 }
michael@0 320
michael@0 321 /**
michael@0 322 * Gets the size of the buttons border this is the union of the normal and disabled borders.
michael@0 323 */
michael@0 324 nsMargin
michael@0 325 nsButtonFrameRenderer::GetButtonInnerFocusMargin()
michael@0 326 {
michael@0 327 nsMargin innerFocusMargin(0,0,0,0);
michael@0 328
michael@0 329 if (mInnerFocusStyle) {
michael@0 330 const nsStyleMargin* margin = mInnerFocusStyle->StyleMargin();
michael@0 331 if (!margin->GetMargin(innerFocusMargin)) {
michael@0 332 NS_NOTYETIMPLEMENTED("percentage margin");
michael@0 333 }
michael@0 334 }
michael@0 335
michael@0 336 return innerFocusMargin;
michael@0 337 }
michael@0 338
michael@0 339 nsMargin
michael@0 340 nsButtonFrameRenderer::GetButtonInnerFocusBorderAndPadding()
michael@0 341 {
michael@0 342 nsMargin result(0,0,0,0);
michael@0 343
michael@0 344 if (mInnerFocusStyle) {
michael@0 345 if (!mInnerFocusStyle->StylePadding()->GetPadding(result)) {
michael@0 346 NS_NOTYETIMPLEMENTED("percentage padding");
michael@0 347 }
michael@0 348 result += mInnerFocusStyle->StyleBorder()->GetComputedBorder();
michael@0 349 }
michael@0 350
michael@0 351 return result;
michael@0 352 }
michael@0 353
michael@0 354 // gets all the focus borders and padding that will be added to the regular border
michael@0 355 nsMargin
michael@0 356 nsButtonFrameRenderer::GetAddedButtonBorderAndPadding()
michael@0 357 {
michael@0 358 return GetButtonOuterFocusBorderAndPadding() + GetButtonInnerFocusMargin() + GetButtonInnerFocusBorderAndPadding();
michael@0 359 }
michael@0 360
michael@0 361 /**
michael@0 362 * Call this when styles change
michael@0 363 */
michael@0 364 void
michael@0 365 nsButtonFrameRenderer::ReResolveStyles(nsPresContext* aPresContext)
michael@0 366 {
michael@0 367 // get all the styles
michael@0 368 nsStyleContext* context = mFrame->StyleContext();
michael@0 369 nsStyleSet *styleSet = aPresContext->StyleSet();
michael@0 370
michael@0 371 // style for the inner such as a dotted line (Windows)
michael@0 372 mInnerFocusStyle =
michael@0 373 styleSet->ProbePseudoElementStyle(mFrame->GetContent()->AsElement(),
michael@0 374 nsCSSPseudoElements::ePseudo_mozFocusInner,
michael@0 375 context);
michael@0 376
michael@0 377 // style for outer focus like a ridged border (MAC).
michael@0 378 mOuterFocusStyle =
michael@0 379 styleSet->ProbePseudoElementStyle(mFrame->GetContent()->AsElement(),
michael@0 380 nsCSSPseudoElements::ePseudo_mozFocusOuter,
michael@0 381 context);
michael@0 382 }
michael@0 383
michael@0 384 nsStyleContext*
michael@0 385 nsButtonFrameRenderer::GetStyleContext(int32_t aIndex) const
michael@0 386 {
michael@0 387 switch (aIndex) {
michael@0 388 case NS_BUTTON_RENDERER_FOCUS_INNER_CONTEXT_INDEX:
michael@0 389 return mInnerFocusStyle;
michael@0 390 case NS_BUTTON_RENDERER_FOCUS_OUTER_CONTEXT_INDEX:
michael@0 391 return mOuterFocusStyle;
michael@0 392 default:
michael@0 393 return nullptr;
michael@0 394 }
michael@0 395 }
michael@0 396
michael@0 397 void
michael@0 398 nsButtonFrameRenderer::SetStyleContext(int32_t aIndex, nsStyleContext* aStyleContext)
michael@0 399 {
michael@0 400 switch (aIndex) {
michael@0 401 case NS_BUTTON_RENDERER_FOCUS_INNER_CONTEXT_INDEX:
michael@0 402 mInnerFocusStyle = aStyleContext;
michael@0 403 break;
michael@0 404 case NS_BUTTON_RENDERER_FOCUS_OUTER_CONTEXT_INDEX:
michael@0 405 mOuterFocusStyle = aStyleContext;
michael@0 406 break;
michael@0 407 }
michael@0 408 }

mercurial