Back to index

lightning-sunbird  0.9+nobinonly
inFlasher.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2001
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Joe Hewitt <hewitt@netscape.com> (original author)
00023  *   Christopher A. Aillon <christopher@aillon.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "inFlasher.h"
00040 #include "inLayoutUtils.h"
00041 
00042 #include "nsIServiceManager.h"
00043 #include "nsIViewManager.h" 
00044 #include "nsIDeviceContext.h"
00045 #include "nsIWidget.h"
00046 #include "nsIPresShell.h"
00047 #include "nsIFrame.h"
00048 #include "nsReadableUtils.h"
00049 
00050 #include "prprf.h"
00051 
00052 static NS_DEFINE_CID(kInspectorCSSUtilsCID, NS_INSPECTORCSSUTILS_CID);
00053 
00055 
00056 inFlasher::inFlasher() :
00057   mColor(NS_RGB(0,0,0)),
00058   mThickness(0),
00059   mInvert(PR_FALSE)
00060 {
00061   mCSSUtils = do_GetService(kInspectorCSSUtilsCID);
00062 }
00063 
00064 inFlasher::~inFlasher()
00065 {
00066 }
00067 
00068 NS_IMPL_ISUPPORTS1(inFlasher, inIFlasher)
00069 
00070 
00071 // inIFlasher
00072 
00073 NS_IMETHODIMP
00074 inFlasher::GetColor(nsAString& aColor)
00075 {
00076   // Copied from nsGenericHTMLElement::ColorToString()
00077   char buf[10];
00078   PR_snprintf(buf, sizeof(buf), "#%02x%02x%02x",
00079               NS_GET_R(mColor), NS_GET_G(mColor), NS_GET_B(mColor));
00080   CopyASCIItoUTF16(buf, aColor);
00081 
00082   return NS_OK;
00083 }
00084 
00085 NS_IMETHODIMP
00086 inFlasher::SetColor(const nsAString& aColor)
00087 {
00088   NS_ENSURE_FALSE(aColor.IsEmpty(), NS_ERROR_ILLEGAL_VALUE);
00089 
00090   nsAutoString colorStr;
00091   colorStr.Assign(aColor);
00092 
00093   if (colorStr.CharAt(0) != '#') {
00094     if (NS_ColorNameToRGB(colorStr, &mColor)) {
00095       return NS_OK;
00096     }
00097   }
00098   else {
00099     colorStr.Cut(0, 1);
00100     if (NS_HexToRGB(colorStr, &mColor)) {
00101       return NS_OK;
00102     }
00103   }
00104 
00105   return NS_ERROR_ILLEGAL_VALUE;
00106 }
00107 
00108 NS_IMETHODIMP
00109 inFlasher::GetThickness(PRUint16 *aThickness)
00110 {
00111   NS_PRECONDITION(aThickness, "Null pointer");
00112   *aThickness = mThickness;
00113   return NS_OK;
00114 }
00115 
00116 NS_IMETHODIMP
00117 inFlasher::SetThickness(PRUint16 aThickness)
00118 {
00119   mThickness = aThickness;
00120   return NS_OK;
00121 }
00122 
00123 NS_IMETHODIMP
00124 inFlasher::GetInvert(PRBool *aInvert)
00125 {
00126   NS_PRECONDITION(aInvert, "Null pointer");
00127   *aInvert = mInvert;
00128   return NS_OK;
00129 }
00130 
00131 NS_IMETHODIMP
00132 inFlasher::SetInvert(PRBool aInvert)
00133 {
00134   mInvert = aInvert;
00135   return NS_OK;
00136 }
00137 
00138 NS_IMETHODIMP
00139 inFlasher::RepaintElement(nsIDOMElement* aElement)
00140 {
00141   nsCOMPtr<nsIDOMWindowInternal> window = inLayoutUtils::GetWindowFor(aElement);
00142   if (!window) return NS_OK;
00143   nsCOMPtr<nsIPresShell> presShell = inLayoutUtils::GetPresShellFor(window);
00144   if (!presShell) return NS_OK;
00145   nsIFrame* frame = inLayoutUtils::GetFrameFor(aElement, presShell);
00146   if (!frame) return NS_OK;
00147 
00148   nsIFrame* parentWithView = frame->GetAncestorWithViewExternal();
00149   if (parentWithView) {
00150     nsIView* view = parentWithView->GetViewExternal();
00151     if (view) {
00152       nsIViewManager* viewManager = view->GetViewManager();
00153       if (viewManager) {
00154         nsRect rect = parentWithView->GetRect();
00155         viewManager->UpdateView(view, rect, NS_VMREFRESH_NO_SYNC);
00156       }
00157     }
00158   }
00159 
00160   return NS_OK;
00161 }
00162 
00163 NS_IMETHODIMP 
00164 inFlasher::DrawElementOutline(nsIDOMElement* aElement)
00165 {
00166   nsCOMPtr<nsIDOMWindowInternal> window = inLayoutUtils::GetWindowFor(aElement);
00167   if (!window) return NS_OK;
00168   nsCOMPtr<nsIPresShell> presShell = inLayoutUtils::GetPresShellFor(window);
00169   if (!presShell) return NS_OK;
00170   
00171   nsPresContext *presContext = presShell->GetPresContext();
00172 
00173   float p2t = presContext->PixelsToTwips();
00174 
00175   PRBool isFirstFrame = PR_TRUE;
00176   nsCOMPtr<nsIRenderingContext> rcontext;
00177   nsIFrame* frame = inLayoutUtils::GetFrameFor(aElement, presShell);
00178   while (frame) {
00179     if (!rcontext) {
00180       presShell->CreateRenderingContext(frame, getter_AddRefs(rcontext));
00181     }
00182     // get view bounds in case this frame is being scrolled
00183     nsRect rect = frame->GetRect();
00184     nsPoint origin = inLayoutUtils::GetClientOrigin(frame);
00185     rect.MoveTo(origin);
00186     mCSSUtils->AdjustRectForMargins(frame, rect);
00187 
00188     if (mInvert) {
00189       rcontext->InvertRect(rect);
00190     }
00191 
00192     frame = frame->GetNextInFlow();
00193 
00194     PRBool isLastFrame = (frame == nsnull);
00195     DrawOutline(rect.x, rect.y, rect.width, rect.height, p2t, rcontext,
00196                 isFirstFrame, isLastFrame);
00197     isFirstFrame = PR_FALSE;
00198   }
00199 
00200   return NS_OK;
00201 }
00202 
00203 NS_IMETHODIMP
00204 inFlasher::ScrollElementIntoView(nsIDOMElement *aElement)
00205 {
00206   NS_PRECONDITION(aElement, "Dude, where's my arg?!");
00207 
00208   nsCOMPtr<nsIDOMWindowInternal> window = inLayoutUtils::GetWindowFor(aElement);
00209   if (!window) {
00210     return NS_OK;
00211   }
00212 
00213   nsCOMPtr<nsIPresShell> presShell = inLayoutUtils::GetPresShellFor(window);
00214   if (!presShell) {
00215     return NS_OK;
00216   }
00217   nsIFrame* frame = inLayoutUtils::GetFrameFor(aElement, presShell);
00218   if (!frame) {
00219     return NS_OK;
00220   }
00221 
00222   presShell->ScrollFrameIntoView(frame,
00223                                  NS_PRESSHELL_SCROLL_ANYWHERE /* VPercent */,
00224                                  NS_PRESSHELL_SCROLL_ANYWHERE /* HPercent */);
00225 
00226   return NS_OK;
00227 }
00228 
00230 // inFlasher
00231 
00232 void
00233 inFlasher::DrawOutline(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight,
00234                        float aP2T, nsIRenderingContext* aRenderContext,
00235                        PRBool aDrawBegin, PRBool aDrawEnd)
00236 {
00237   aRenderContext->SetColor(mColor);
00238 
00239   DrawLine(aX, aY, aWidth, DIR_HORIZONTAL, BOUND_OUTER, aP2T, aRenderContext);
00240   if (aDrawBegin) {
00241     DrawLine(aX, aY, aHeight, DIR_VERTICAL, BOUND_OUTER, aP2T, aRenderContext);
00242   }
00243   DrawLine(aX, aY+aHeight, aWidth, DIR_HORIZONTAL, BOUND_INNER, aP2T, aRenderContext);
00244   if (aDrawEnd) {
00245     DrawLine(aX+aWidth, aY, aHeight, DIR_VERTICAL, BOUND_INNER, aP2T, aRenderContext);
00246   }
00247 }
00248 
00249 void
00250 inFlasher::DrawLine(nscoord aX, nscoord aY, nscoord aLength,
00251                     PRBool aDir, PRBool aBounds, float aP2T,
00252                     nsIRenderingContext* aRenderContext)
00253 {
00254   nscoord thickTwips = NSIntPixelsToTwips(mThickness, aP2T);
00255   if (aDir) { // horizontal
00256     aRenderContext->FillRect(aX, aY+(aBounds?0:-thickTwips), aLength, thickTwips);
00257   } else { // vertical
00258     aRenderContext->FillRect(aX+(aBounds?0:-thickTwips), aY, thickTwips, aLength);
00259   }
00260 }