Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "inFlasher.h" |
michael@0 | 6 | #include "inLayoutUtils.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsIDOMElement.h" |
michael@0 | 9 | #include "nsIServiceManager.h" |
michael@0 | 10 | #include "nsIPresShell.h" |
michael@0 | 11 | #include "nsIFrame.h" |
michael@0 | 12 | #include "nsIWidget.h" |
michael@0 | 13 | #include "nsReadableUtils.h" |
michael@0 | 14 | #include "nsRenderingContext.h" |
michael@0 | 15 | #include "nsIDOMWindow.h" |
michael@0 | 16 | #include "nsIContent.h" |
michael@0 | 17 | |
michael@0 | 18 | #include "prprf.h" |
michael@0 | 19 | |
michael@0 | 20 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 21 | |
michael@0 | 22 | inFlasher::inFlasher() : |
michael@0 | 23 | mColor(NS_RGB(0,0,0)), |
michael@0 | 24 | mThickness(0), |
michael@0 | 25 | mInvert(false) |
michael@0 | 26 | { |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | inFlasher::~inFlasher() |
michael@0 | 30 | { |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | NS_IMPL_ISUPPORTS(inFlasher, inIFlasher) |
michael@0 | 34 | |
michael@0 | 35 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 36 | // inIFlasher |
michael@0 | 37 | |
michael@0 | 38 | NS_IMETHODIMP |
michael@0 | 39 | inFlasher::GetColor(nsAString& aColor) |
michael@0 | 40 | { |
michael@0 | 41 | // Copied from nsGenericHTMLElement::ColorToString() |
michael@0 | 42 | char buf[10]; |
michael@0 | 43 | PR_snprintf(buf, sizeof(buf), "#%02x%02x%02x", |
michael@0 | 44 | NS_GET_R(mColor), NS_GET_G(mColor), NS_GET_B(mColor)); |
michael@0 | 45 | CopyASCIItoUTF16(buf, aColor); |
michael@0 | 46 | |
michael@0 | 47 | return NS_OK; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | NS_IMETHODIMP |
michael@0 | 51 | inFlasher::SetColor(const nsAString& aColor) |
michael@0 | 52 | { |
michael@0 | 53 | NS_ENSURE_FALSE(aColor.IsEmpty(), NS_ERROR_ILLEGAL_VALUE); |
michael@0 | 54 | |
michael@0 | 55 | nsAutoString colorStr; |
michael@0 | 56 | colorStr.Assign(aColor); |
michael@0 | 57 | |
michael@0 | 58 | if (colorStr.CharAt(0) != '#') { |
michael@0 | 59 | if (NS_ColorNameToRGB(colorStr, &mColor)) { |
michael@0 | 60 | return NS_OK; |
michael@0 | 61 | } |
michael@0 | 62 | } |
michael@0 | 63 | else { |
michael@0 | 64 | colorStr.Cut(0, 1); |
michael@0 | 65 | if (NS_HexToRGB(colorStr, &mColor)) { |
michael@0 | 66 | return NS_OK; |
michael@0 | 67 | } |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | return NS_ERROR_ILLEGAL_VALUE; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | NS_IMETHODIMP |
michael@0 | 74 | inFlasher::GetThickness(uint16_t *aThickness) |
michael@0 | 75 | { |
michael@0 | 76 | NS_PRECONDITION(aThickness, "Null pointer"); |
michael@0 | 77 | *aThickness = mThickness; |
michael@0 | 78 | return NS_OK; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | NS_IMETHODIMP |
michael@0 | 82 | inFlasher::SetThickness(uint16_t aThickness) |
michael@0 | 83 | { |
michael@0 | 84 | mThickness = aThickness; |
michael@0 | 85 | return NS_OK; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | NS_IMETHODIMP |
michael@0 | 89 | inFlasher::GetInvert(bool *aInvert) |
michael@0 | 90 | { |
michael@0 | 91 | NS_PRECONDITION(aInvert, "Null pointer"); |
michael@0 | 92 | *aInvert = mInvert; |
michael@0 | 93 | return NS_OK; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | NS_IMETHODIMP |
michael@0 | 97 | inFlasher::SetInvert(bool aInvert) |
michael@0 | 98 | { |
michael@0 | 99 | mInvert = aInvert; |
michael@0 | 100 | return NS_OK; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | NS_IMETHODIMP |
michael@0 | 104 | inFlasher::RepaintElement(nsIDOMElement* aElement) |
michael@0 | 105 | { |
michael@0 | 106 | NS_ENSURE_ARG_POINTER(aElement); |
michael@0 | 107 | nsIFrame* frame = inLayoutUtils::GetFrameFor(aElement); |
michael@0 | 108 | if (!frame) return NS_OK; |
michael@0 | 109 | |
michael@0 | 110 | frame->InvalidateFrame(); |
michael@0 | 111 | |
michael@0 | 112 | return NS_OK; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | NS_IMETHODIMP |
michael@0 | 116 | inFlasher::DrawElementOutline(nsIDOMElement* aElement) |
michael@0 | 117 | { |
michael@0 | 118 | NS_ENSURE_ARG_POINTER(aElement); |
michael@0 | 119 | nsCOMPtr<nsIDOMWindow> window = inLayoutUtils::GetWindowFor(aElement); |
michael@0 | 120 | if (!window) return NS_OK; |
michael@0 | 121 | nsCOMPtr<nsIPresShell> presShell = inLayoutUtils::GetPresShellFor(window); |
michael@0 | 122 | if (!presShell) return NS_OK; |
michael@0 | 123 | |
michael@0 | 124 | nsIFrame* frame = inLayoutUtils::GetFrameFor(aElement); |
michael@0 | 125 | |
michael@0 | 126 | bool isFirstFrame = true; |
michael@0 | 127 | |
michael@0 | 128 | while (frame) { |
michael@0 | 129 | nsPoint offset; |
michael@0 | 130 | nsIWidget* widget = frame->GetNearestWidget(offset); |
michael@0 | 131 | if (widget) { |
michael@0 | 132 | nsRefPtr<nsRenderingContext> rcontext = new nsRenderingContext(); |
michael@0 | 133 | rcontext->Init(frame->PresContext()->DeviceContext(), |
michael@0 | 134 | widget->GetThebesSurface()); |
michael@0 | 135 | |
michael@0 | 136 | nsRect rect(offset, frame->GetSize()); |
michael@0 | 137 | if (mInvert) { |
michael@0 | 138 | rcontext->InvertRect(rect); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | bool isLastFrame = frame->GetNextContinuation() == nullptr; |
michael@0 | 142 | DrawOutline(rect.x, rect.y, rect.width, rect.height, rcontext, |
michael@0 | 143 | isFirstFrame, isLastFrame); |
michael@0 | 144 | isFirstFrame = false; |
michael@0 | 145 | } |
michael@0 | 146 | frame = frame->GetNextContinuation(); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | return NS_OK; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | NS_IMETHODIMP |
michael@0 | 153 | inFlasher::ScrollElementIntoView(nsIDOMElement *aElement) |
michael@0 | 154 | { |
michael@0 | 155 | NS_ENSURE_ARG_POINTER(aElement); |
michael@0 | 156 | nsCOMPtr<nsIDOMWindow> window = inLayoutUtils::GetWindowFor(aElement); |
michael@0 | 157 | if (!window) { |
michael@0 | 158 | return NS_OK; |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | nsCOMPtr<nsIPresShell> presShell = inLayoutUtils::GetPresShellFor(window); |
michael@0 | 162 | if (!presShell) { |
michael@0 | 163 | return NS_OK; |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | nsCOMPtr<nsIContent> content = do_QueryInterface(aElement); |
michael@0 | 167 | presShell->ScrollContentIntoView(content, |
michael@0 | 168 | nsIPresShell::ScrollAxis(), |
michael@0 | 169 | nsIPresShell::ScrollAxis(), |
michael@0 | 170 | nsIPresShell::SCROLL_OVERFLOW_HIDDEN); |
michael@0 | 171 | |
michael@0 | 172 | return NS_OK; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 176 | // inFlasher |
michael@0 | 177 | |
michael@0 | 178 | void |
michael@0 | 179 | inFlasher::DrawOutline(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight, |
michael@0 | 180 | nsRenderingContext* aRenderContext, |
michael@0 | 181 | bool aDrawBegin, bool aDrawEnd) |
michael@0 | 182 | { |
michael@0 | 183 | aRenderContext->SetColor(mColor); |
michael@0 | 184 | |
michael@0 | 185 | DrawLine(aX, aY, aWidth, DIR_HORIZONTAL, BOUND_OUTER, aRenderContext); |
michael@0 | 186 | if (aDrawBegin) { |
michael@0 | 187 | DrawLine(aX, aY, aHeight, DIR_VERTICAL, BOUND_OUTER, aRenderContext); |
michael@0 | 188 | } |
michael@0 | 189 | DrawLine(aX, aY+aHeight, aWidth, DIR_HORIZONTAL, BOUND_INNER, aRenderContext); |
michael@0 | 190 | if (aDrawEnd) { |
michael@0 | 191 | DrawLine(aX+aWidth, aY, aHeight, DIR_VERTICAL, BOUND_INNER, aRenderContext); |
michael@0 | 192 | } |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | void |
michael@0 | 196 | inFlasher::DrawLine(nscoord aX, nscoord aY, nscoord aLength, |
michael@0 | 197 | bool aDir, bool aBounds, |
michael@0 | 198 | nsRenderingContext* aRenderContext) |
michael@0 | 199 | { |
michael@0 | 200 | nscoord thickTwips = nsPresContext::CSSPixelsToAppUnits(mThickness); |
michael@0 | 201 | if (aDir) { // horizontal |
michael@0 | 202 | aRenderContext->FillRect(aX, aY+(aBounds?0:-thickTwips), aLength, thickTwips); |
michael@0 | 203 | } else { // vertical |
michael@0 | 204 | aRenderContext->FillRect(aX+(aBounds?0:-thickTwips), aY, thickTwips, aLength); |
michael@0 | 205 | } |
michael@0 | 206 | } |