Back to index

lightning-sunbird  0.9+nobinonly
nsFormControlHelper.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include "nsCOMPtr.h"
00038 #include "nsFormControlHelper.h"
00039 #include "nsHTMLParts.h"
00040 #include "nsGenericHTMLElement.h"
00041 #include "nsIRenderingContext.h"
00042 #include "nsIPresShell.h"
00043 #include "nsPresContext.h"
00044 #include "nsStyleContext.h"
00045 #include "nsLeafFrame.h"
00046 #include "nsCSSRendering.h"
00047 #include "nsIView.h"
00048 #include "nsIViewManager.h"
00049 #include "nsCoord.h"
00050 #include "nsWidgetsCID.h"
00051 #include "nsViewsCID.h"
00052 #include "nsIComponentManager.h"
00053 #include "nsGUIEvent.h"
00054 #include "nsIFontMetrics.h"
00055 #include "nsIFormControl.h"
00056 #include "nsIDeviceContext.h"
00057 #include "nsHTMLAtoms.h"
00058 #include "nsIButton.h"  // remove this when GetCID is pure virtual
00059 #include "nsICheckButton.h"  //remove this
00060 #include "nsITextWidget.h"  //remove this
00061 #include "nsISupports.h"
00062 #include "nsStyleConsts.h"
00063 #include "nsUnitConversion.h"
00064 #include "nsIContent.h"
00065 #include "nsIDOMHTMLInputElement.h"
00066 #include "nsNetCID.h"
00067 
00068 // Needed for Localization
00069 #include "nsIServiceManager.h"
00070 #include "nsIStringBundle.h"
00071 #include "nsITextContent.h"
00072 #include "nsISupportsArray.h"
00073 #include "nsXPIDLString.h"
00074 
00075 static NS_DEFINE_CID(kStringBundleServiceCID,  NS_STRINGBUNDLESERVICE_CID);
00076 // done I10N
00077 
00078 
00079 // For figuring out the "WRAP" property
00080 // See GetWrapPropertyEnum for details
00081 #define kTextControl_Wrap_Soft "SOFT"
00082 #define kTextControl_Wrap_Hard "HARD"
00083 #define kTextControl_Wrap_Off  "OFF"
00084 
00085 
00086 MOZ_DECL_CTOR_COUNTER(nsFormControlHelper)
00087 
00088 nsFormControlHelper::nsFormControlHelper()
00089 {
00090   MOZ_COUNT_CTOR(nsFormControlHelper);
00091 }
00092 
00093 nsFormControlHelper::~nsFormControlHelper()
00094 {
00095   MOZ_COUNT_DTOR(nsFormControlHelper);
00096 }
00097 
00098 void nsFormControlHelper::PlatformToDOMLineBreaks(nsString &aString)
00099 {
00100   // Windows linebreaks: Map CRLF to LF:
00101   aString.ReplaceSubstring(NS_LITERAL_STRING("\r\n").get(), NS_LITERAL_STRING("\n").get());
00102 
00103   // Mac linebreaks: Map any remaining CR to LF:
00104   aString.ReplaceSubstring(NS_LITERAL_STRING("\r").get(), NS_LITERAL_STRING("\n").get());
00105 }
00106 
00107 PRBool nsFormControlHelper::GetBool(const nsAString& aValue)
00108 {
00109   return aValue.Equals(NS_STRING_TRUE);
00110 }
00111 
00112 void nsFormControlHelper::GetBoolString(const PRBool aValue,
00113                                         nsAString& aResult)
00114 {
00115   if (aValue)
00116     aResult.Assign(NS_STRING_TRUE); 
00117   else
00118     aResult.Assign(NS_STRING_FALSE);
00119 }
00120 
00121 
00122 nsresult nsFormControlHelper::GetFrameFontFM(nsIFrame* aFrame,
00123                                              nsIFontMetrics** aFontMet)
00124 {
00125   nsStyleContext* sc = aFrame->GetStyleContext();
00126   return aFrame->GetPresContext()->DeviceContext()->
00127     GetMetricsFor(sc->GetStyleFont()->mFont,
00128                   sc->GetStyleVisibility()->mLangGroup,
00129                   *aFontMet);
00130 }
00131 
00132 nsresult
00133 nsFormControlHelper::GetWrapProperty(nsIContent * aContent, nsString &aOutValue)
00134 {
00135   if (aContent->IsContentOfType(nsIContent::eHTML)) {
00136     return aContent->GetAttr(kNameSpaceID_None, nsHTMLAtoms::wrap, aOutValue);
00137   }
00138 
00139   aOutValue.Truncate();
00140 
00141   return NS_CONTENT_ATTR_NOT_THERE;
00142 }
00143 
00144 
00145 nsresult 
00146 nsFormControlHelper::GetWrapPropertyEnum(nsIContent * aContent, nsHTMLTextWrap& aWrapProp)
00147 {
00148   // soft is the default; "physical" defaults to soft as well because all other
00149   // browsers treat it that way and there is no real reason to maintain physical
00150   // and virtual as separate entities if no one else does.  Only hard and off
00151   // do anything different.
00152   aWrapProp = eHTMLTextWrap_Soft; // the default
00153   
00154   nsAutoString wrap;
00155   nsresult rv = GetWrapProperty(aContent, wrap);
00156 
00157   if (rv != NS_CONTENT_ATTR_NOT_THERE) {
00158 
00159     if (wrap.EqualsIgnoreCase(kTextControl_Wrap_Hard)) {
00160       aWrapProp = eHTMLTextWrap_Hard;
00161     } else if (wrap.EqualsIgnoreCase(kTextControl_Wrap_Off)) {
00162       aWrapProp = eHTMLTextWrap_Off;
00163     }
00164   }
00165   return rv;
00166 }
00167 
00168 //
00169 //-------------------------------------------------------------------------------------
00170 // Utility methods for rendering Form Elements using GFX
00171 //-------------------------------------------------------------------------------------
00172 //
00173 
00174 void 
00175 nsFormControlHelper::PaintLine(nsIRenderingContext& aRenderingContext, 
00176                               nscoord aSX, nscoord aSY, nscoord aEX, nscoord aEY, 
00177                               PRBool aHorz, nscoord aWidth, nscoord aOnePixel)
00178 {
00179   
00180   nsPoint p[5];
00181   if (aHorz) {
00182     aEX++;
00183     p[0].x = nscoord(float(aSX)*aOnePixel);
00184     p[0].y = nscoord(float(aSY)*aOnePixel);
00185     p[1].x = nscoord(float(aEX)*aOnePixel);
00186     p[1].y = nscoord(float(aEY)*aOnePixel);
00187     p[2].x = nscoord(float(aEX)*aOnePixel);
00188     p[2].y = nscoord(float(aEY+1)*aOnePixel);
00189     p[3].x = nscoord(float(aSX)*aOnePixel);
00190     p[3].y = nscoord(float(aSY+1)*aOnePixel);
00191     p[4].x = nscoord(float(aSX)*aOnePixel);
00192     p[4].y = nscoord(float(aSY)*aOnePixel);
00193   } else {
00194     aEY++;
00195     p[0].x = nscoord(float(aSX)*aOnePixel);
00196     p[0].y = nscoord(float(aSY)*aOnePixel);
00197     p[1].x = nscoord(float(aEX)*aOnePixel);
00198     p[1].y = nscoord(float(aEY)*aOnePixel);
00199     p[2].x = nscoord(float(aEX+1)*aOnePixel);
00200     p[2].y = nscoord(float(aEY)*aOnePixel);
00201     p[3].x = nscoord(float(aSX+1)*aOnePixel);
00202     p[3].y = nscoord(float(aSY)*aOnePixel);
00203     p[4].x = nscoord(float(aSX)*aOnePixel);
00204     p[4].y = nscoord(float(aSY)*aOnePixel);
00205   }
00206   aRenderingContext.FillPolygon(p, 5);
00207 
00208 }
00209 
00210 //---------------------------------------------------------------------------
00211 void 
00212 nsFormControlHelper::SetupPoints(PRUint32 aNumberOfPoints, nscoord* aPoints, nsPoint* aPolygon, nscoord aScaleFactor, nscoord aX, nscoord aY,
00213                                 nscoord aCenterX, nscoord aCenterY) 
00214 {
00215   const nscoord offsetX = aCenterX * aScaleFactor;
00216   const nscoord offsetY = aCenterY * aScaleFactor;
00217 
00218   PRUint32 i = 0;
00219   PRUint32 count = 0;
00220   for (i = 0; i < aNumberOfPoints; i++) {
00221     aPolygon[i].x = (aPoints[count] * aScaleFactor) + aX - offsetX;
00222     count++;
00223     aPolygon[i].y = (aPoints[count] * aScaleFactor) + aY - offsetY;
00224     count++;
00225   }
00226 }
00227 
00228 
00229 void
00230 nsFormControlHelper::PaintFixedSizeCheckMark(nsIRenderingContext& aRenderingContext,
00231                          float aPixelsToTwips)
00232 {
00233 
00234     // Offsets to x,y location, These offsets are used to place the checkmark in the middle
00235     // of it's 12X12 pixel box.
00236   const PRUint32 ox = 3;
00237   const PRUint32 oy = 3;
00238 
00239   nscoord onePixel = NSIntPixelsToTwips(1, aPixelsToTwips);
00240 
00241     // Draw checkmark using a series of rectangles. This builds an replica of the
00242     // way the checkmark looks under Windows. Using a polygon does not correctly 
00243     // represent a checkmark under Windows. This is due to round-off error in the
00244     // Twips to Pixel conversions.
00245   PaintLine(aRenderingContext, 0 + ox, 2 + oy, 0 + ox, 4 + oy, PR_FALSE, 1, onePixel);
00246   PaintLine(aRenderingContext, 1 + ox, 3 + oy, 1 + ox, 5 + oy, PR_FALSE, 1, onePixel);
00247   PaintLine(aRenderingContext, 2 + ox, 4 + oy, 2 + ox, 6 + oy, PR_FALSE, 1, onePixel);
00248   PaintLine(aRenderingContext, 3 + ox, 3 + oy, 3 + ox, 5 + oy, PR_FALSE, 1, onePixel);
00249   PaintLine(aRenderingContext, 4 + ox, 2 + oy, 4 + ox, 4 + oy, PR_FALSE, 1, onePixel);
00250   PaintLine(aRenderingContext, 5 + ox, 1 + oy, 5 + ox, 3 + oy, PR_FALSE, 1, onePixel);
00251   PaintLine(aRenderingContext, 6 + ox, 0 + oy, 6 + ox, 2 + oy, PR_FALSE, 1, onePixel);
00252 }
00253 
00254 void
00255 nsFormControlHelper::PaintFixedSizeCheckMarkBorder(nsIRenderingContext& aRenderingContext,
00256                          float aPixelsToTwips, const nsStyleColor& aBackgroundColor)
00257 {
00258     // Offsets to x,y location
00259   PRUint32 ox = 0;
00260   PRUint32 oy = 0;
00261 
00262   nscoord onePixel = NSIntPixelsToTwips(1, aPixelsToTwips);
00263   nscoord twelvePixels = NSIntPixelsToTwips(12, aPixelsToTwips);
00264 
00265     // Draw Background
00266   aRenderingContext.SetColor(aBackgroundColor.mColor);
00267   nsRect rect(0, 0, twelvePixels, twelvePixels);
00268   aRenderingContext.FillRect(rect);
00269 
00270     // Draw Border
00271   aRenderingContext.SetColor(NS_RGB(128, 128, 128));
00272   PaintLine(aRenderingContext, 0 + ox, 0 + oy, 11 + ox, 0 + oy, PR_TRUE, 1, onePixel);
00273   PaintLine(aRenderingContext, 0 + ox, 0 + oy, 0 + ox, 11 + oy, PR_FALSE, 1, onePixel);
00274 
00275   aRenderingContext.SetColor(NS_RGB(192, 192, 192));
00276   PaintLine(aRenderingContext, 1 + ox, 11 + oy, 11 + ox, 11 + oy, PR_TRUE, 1, onePixel);
00277   PaintLine(aRenderingContext, 11 + ox, 1 + oy, 11 + ox, 11 + oy, PR_FALSE, 1, onePixel);
00278 
00279   aRenderingContext.SetColor(NS_RGB(0, 0, 0));
00280   PaintLine(aRenderingContext, 1 + ox, 1 + oy, 10 + ox, 1 + oy, PR_TRUE, 1, onePixel);
00281   PaintLine(aRenderingContext, 1 + ox, 1 + oy, 1 + ox, 10 + oy, PR_FALSE, 1, onePixel);
00282 
00283 }
00284 
00285 //Draw a checkmark in any size.
00286 void
00287 nsFormControlHelper::PaintCheckMark(nsIRenderingContext& aRenderingContext,
00288                                     float aPixelsToTwips, const nsRect & aRect)
00289 {
00290  // Width and height of the fixed size checkmark in TWIPS.
00291   const PRInt32 fixedSizeCheckmarkWidth = 165;
00292   const PRInt32 fixedSizeCheckmarkHeight = 165;
00293 
00294   if ((fixedSizeCheckmarkWidth == aRect.width)  &&
00295       (fixedSizeCheckmarkHeight == aRect.height)) {
00296       // Standard size, so draw a fixed size check mark instead of a scaled check mark.
00297       PaintFixedSizeCheckMark(aRenderingContext, aPixelsToTwips);
00298     return;
00299   }
00300 
00301   const PRUint32 checkpoints = 7;
00302   const PRUint32 checksize   = 9; //This is value is determined by added 2 units to the end
00303                                 //of the 7X& pixel rectangle below to provide some white space
00304                                 //around the checkmark when it is rendered.
00305 
00306     // Points come from the coordinates on a 7X7 pixels 
00307     // box with 0,0 at the lower left. 
00308   nscoord checkedPolygonDef[] = {0,2, 2,4, 6,0 , 6,2, 2,6, 0,4, 0,2 };
00309     // Location of the center point of the checkmark
00310   const PRUint32 centerx = 3;
00311   const PRUint32 centery = 3;
00312   
00313   nsPoint checkedPolygon[checkpoints];
00314   PRUint32 defIndex = 0;
00315   PRUint32 polyIndex = 0;
00316 
00317      // Scale the checkmark based on the smallest dimension
00318   PRUint32 size = aRect.width / checksize;
00319   if (aRect.height < aRect.width) {
00320    size = aRect.height / checksize;
00321   }
00322   
00323     // Center and offset each point in the polygon definition.
00324   for (defIndex = 0; defIndex < (checkpoints * 2); defIndex++) {
00325     checkedPolygon[polyIndex].x = nscoord((((checkedPolygonDef[defIndex]) - centerx) * (size)) + (aRect.width / 2) + aRect.x);
00326     defIndex++;
00327     checkedPolygon[polyIndex].y = nscoord((((checkedPolygonDef[defIndex]) - centery) * (size)) + (aRect.height / 2) + aRect.y);
00328     polyIndex++;
00329   }
00330   
00331   aRenderingContext.FillPolygon(checkedPolygon, checkpoints);
00332 }
00333 
00334 nsresult
00335 nsFormControlHelper::GetName(nsIContent* aContent, nsAString* aResult)
00336 {
00337   NS_PRECONDITION(aResult, "Null pointer bad!");
00338   if (!aContent->IsContentOfType(nsIContent::eHTML))
00339     return NS_ERROR_FAILURE;
00340 
00341   return aContent->GetAttr(kNameSpaceID_None, nsHTMLAtoms::name, *aResult);
00342 }
00343 
00344 PRInt32
00345 nsFormControlHelper::GetType(nsIContent* aContent)
00346 {
00347   nsCOMPtr<nsIFormControl> formControl(do_QueryInterface(aContent));
00348   if (formControl) {
00349     return formControl->GetType();
00350   }
00351 
00352   NS_ERROR("Form control not implementing nsIFormControl, assuming TEXT type");
00353   return NS_FORM_INPUT_TEXT;
00354 }
00355 
00356 nsresult
00357 nsFormControlHelper::GetValueAttr(nsIContent* aContent, nsAString* aResult)
00358 {
00359   NS_PRECONDITION(aResult, "Null pointer bad!");
00360   if (!aContent->IsContentOfType(nsIContent::eHTML))
00361     return NS_ERROR_FAILURE;
00362 
00363   return aContent->GetAttr(kNameSpaceID_None, nsHTMLAtoms::value, *aResult);
00364 }
00365 
00366 //----------------------------------------------------------------------------------
00367 
00368 nsresult
00369 nsFormControlHelper::Reset(nsIFrame* aFrame, nsPresContext* aPresContext)
00370 {
00371   nsCOMPtr<nsIFormControl> control = do_QueryInterface(aFrame->GetContent());
00372   if (control) {
00373     control->Reset();
00374     return NS_OK;
00375   }
00376 
00377   return NS_ERROR_FAILURE;
00378 }
00379 
00380 void
00381 nsFormControlHelper::StyleChangeReflow(nsPresContext* aPresContext,
00382                                        nsIFrame* aFrame)
00383 {
00384   aPresContext->PresShell()->AppendReflowCommand(aFrame,
00385                                                  eReflowType_StyleChanged,
00386                                                  nsnull);
00387 }
00388 
00389