1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/layout/inspector/inFlasher.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,206 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "inFlasher.h" 1.9 +#include "inLayoutUtils.h" 1.10 + 1.11 +#include "nsIDOMElement.h" 1.12 +#include "nsIServiceManager.h" 1.13 +#include "nsIPresShell.h" 1.14 +#include "nsIFrame.h" 1.15 +#include "nsIWidget.h" 1.16 +#include "nsReadableUtils.h" 1.17 +#include "nsRenderingContext.h" 1.18 +#include "nsIDOMWindow.h" 1.19 +#include "nsIContent.h" 1.20 + 1.21 +#include "prprf.h" 1.22 + 1.23 +/////////////////////////////////////////////////////////////////////////////// 1.24 + 1.25 +inFlasher::inFlasher() : 1.26 + mColor(NS_RGB(0,0,0)), 1.27 + mThickness(0), 1.28 + mInvert(false) 1.29 +{ 1.30 +} 1.31 + 1.32 +inFlasher::~inFlasher() 1.33 +{ 1.34 +} 1.35 + 1.36 +NS_IMPL_ISUPPORTS(inFlasher, inIFlasher) 1.37 + 1.38 +/////////////////////////////////////////////////////////////////////////////// 1.39 +// inIFlasher 1.40 + 1.41 +NS_IMETHODIMP 1.42 +inFlasher::GetColor(nsAString& aColor) 1.43 +{ 1.44 + // Copied from nsGenericHTMLElement::ColorToString() 1.45 + char buf[10]; 1.46 + PR_snprintf(buf, sizeof(buf), "#%02x%02x%02x", 1.47 + NS_GET_R(mColor), NS_GET_G(mColor), NS_GET_B(mColor)); 1.48 + CopyASCIItoUTF16(buf, aColor); 1.49 + 1.50 + return NS_OK; 1.51 +} 1.52 + 1.53 +NS_IMETHODIMP 1.54 +inFlasher::SetColor(const nsAString& aColor) 1.55 +{ 1.56 + NS_ENSURE_FALSE(aColor.IsEmpty(), NS_ERROR_ILLEGAL_VALUE); 1.57 + 1.58 + nsAutoString colorStr; 1.59 + colorStr.Assign(aColor); 1.60 + 1.61 + if (colorStr.CharAt(0) != '#') { 1.62 + if (NS_ColorNameToRGB(colorStr, &mColor)) { 1.63 + return NS_OK; 1.64 + } 1.65 + } 1.66 + else { 1.67 + colorStr.Cut(0, 1); 1.68 + if (NS_HexToRGB(colorStr, &mColor)) { 1.69 + return NS_OK; 1.70 + } 1.71 + } 1.72 + 1.73 + return NS_ERROR_ILLEGAL_VALUE; 1.74 +} 1.75 + 1.76 +NS_IMETHODIMP 1.77 +inFlasher::GetThickness(uint16_t *aThickness) 1.78 +{ 1.79 + NS_PRECONDITION(aThickness, "Null pointer"); 1.80 + *aThickness = mThickness; 1.81 + return NS_OK; 1.82 +} 1.83 + 1.84 +NS_IMETHODIMP 1.85 +inFlasher::SetThickness(uint16_t aThickness) 1.86 +{ 1.87 + mThickness = aThickness; 1.88 + return NS_OK; 1.89 +} 1.90 + 1.91 +NS_IMETHODIMP 1.92 +inFlasher::GetInvert(bool *aInvert) 1.93 +{ 1.94 + NS_PRECONDITION(aInvert, "Null pointer"); 1.95 + *aInvert = mInvert; 1.96 + return NS_OK; 1.97 +} 1.98 + 1.99 +NS_IMETHODIMP 1.100 +inFlasher::SetInvert(bool aInvert) 1.101 +{ 1.102 + mInvert = aInvert; 1.103 + return NS_OK; 1.104 +} 1.105 + 1.106 +NS_IMETHODIMP 1.107 +inFlasher::RepaintElement(nsIDOMElement* aElement) 1.108 +{ 1.109 + NS_ENSURE_ARG_POINTER(aElement); 1.110 + nsIFrame* frame = inLayoutUtils::GetFrameFor(aElement); 1.111 + if (!frame) return NS_OK; 1.112 + 1.113 + frame->InvalidateFrame(); 1.114 + 1.115 + return NS_OK; 1.116 +} 1.117 + 1.118 +NS_IMETHODIMP 1.119 +inFlasher::DrawElementOutline(nsIDOMElement* aElement) 1.120 +{ 1.121 + NS_ENSURE_ARG_POINTER(aElement); 1.122 + nsCOMPtr<nsIDOMWindow> window = inLayoutUtils::GetWindowFor(aElement); 1.123 + if (!window) return NS_OK; 1.124 + nsCOMPtr<nsIPresShell> presShell = inLayoutUtils::GetPresShellFor(window); 1.125 + if (!presShell) return NS_OK; 1.126 + 1.127 + nsIFrame* frame = inLayoutUtils::GetFrameFor(aElement); 1.128 + 1.129 + bool isFirstFrame = true; 1.130 + 1.131 + while (frame) { 1.132 + nsPoint offset; 1.133 + nsIWidget* widget = frame->GetNearestWidget(offset); 1.134 + if (widget) { 1.135 + nsRefPtr<nsRenderingContext> rcontext = new nsRenderingContext(); 1.136 + rcontext->Init(frame->PresContext()->DeviceContext(), 1.137 + widget->GetThebesSurface()); 1.138 + 1.139 + nsRect rect(offset, frame->GetSize()); 1.140 + if (mInvert) { 1.141 + rcontext->InvertRect(rect); 1.142 + } 1.143 + 1.144 + bool isLastFrame = frame->GetNextContinuation() == nullptr; 1.145 + DrawOutline(rect.x, rect.y, rect.width, rect.height, rcontext, 1.146 + isFirstFrame, isLastFrame); 1.147 + isFirstFrame = false; 1.148 + } 1.149 + frame = frame->GetNextContinuation(); 1.150 + } 1.151 + 1.152 + return NS_OK; 1.153 +} 1.154 + 1.155 +NS_IMETHODIMP 1.156 +inFlasher::ScrollElementIntoView(nsIDOMElement *aElement) 1.157 +{ 1.158 + NS_ENSURE_ARG_POINTER(aElement); 1.159 + nsCOMPtr<nsIDOMWindow> window = inLayoutUtils::GetWindowFor(aElement); 1.160 + if (!window) { 1.161 + return NS_OK; 1.162 + } 1.163 + 1.164 + nsCOMPtr<nsIPresShell> presShell = inLayoutUtils::GetPresShellFor(window); 1.165 + if (!presShell) { 1.166 + return NS_OK; 1.167 + } 1.168 + 1.169 + nsCOMPtr<nsIContent> content = do_QueryInterface(aElement); 1.170 + presShell->ScrollContentIntoView(content, 1.171 + nsIPresShell::ScrollAxis(), 1.172 + nsIPresShell::ScrollAxis(), 1.173 + nsIPresShell::SCROLL_OVERFLOW_HIDDEN); 1.174 + 1.175 + return NS_OK; 1.176 +} 1.177 + 1.178 +/////////////////////////////////////////////////////////////////////////////// 1.179 +// inFlasher 1.180 + 1.181 +void 1.182 +inFlasher::DrawOutline(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight, 1.183 + nsRenderingContext* aRenderContext, 1.184 + bool aDrawBegin, bool aDrawEnd) 1.185 +{ 1.186 + aRenderContext->SetColor(mColor); 1.187 + 1.188 + DrawLine(aX, aY, aWidth, DIR_HORIZONTAL, BOUND_OUTER, aRenderContext); 1.189 + if (aDrawBegin) { 1.190 + DrawLine(aX, aY, aHeight, DIR_VERTICAL, BOUND_OUTER, aRenderContext); 1.191 + } 1.192 + DrawLine(aX, aY+aHeight, aWidth, DIR_HORIZONTAL, BOUND_INNER, aRenderContext); 1.193 + if (aDrawEnd) { 1.194 + DrawLine(aX+aWidth, aY, aHeight, DIR_VERTICAL, BOUND_INNER, aRenderContext); 1.195 + } 1.196 +} 1.197 + 1.198 +void 1.199 +inFlasher::DrawLine(nscoord aX, nscoord aY, nscoord aLength, 1.200 + bool aDir, bool aBounds, 1.201 + nsRenderingContext* aRenderContext) 1.202 +{ 1.203 + nscoord thickTwips = nsPresContext::CSSPixelsToAppUnits(mThickness); 1.204 + if (aDir) { // horizontal 1.205 + aRenderContext->FillRect(aX, aY+(aBounds?0:-thickTwips), aLength, thickTwips); 1.206 + } else { // vertical 1.207 + aRenderContext->FillRect(aX+(aBounds?0:-thickTwips), aY, thickTwips, aLength); 1.208 + } 1.209 +}