Back to index

lightning-sunbird  0.9+nobinonly
nsNativeTheme.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) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Brian Ryner <bryner@brianryner.com>  (Original Author)
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 "nsNativeTheme.h"
00040 #include "nsIDocument.h"
00041 #include "nsIContent.h"
00042 #include "nsIFrame.h"
00043 #include "nsIPresShell.h"
00044 #include "nsPresContext.h"
00045 #include "nsIEventStateManager.h"
00046 #include "nsString.h"
00047 #include "nsINameSpaceManager.h"
00048 #include "nsIDOMHTMLInputElement.h"
00049 #include "nsILookAndFeel.h"
00050 #include "nsThemeConstants.h"
00051 #include "nsIComponentManager.h"
00052 
00053 nsMargin nsNativeTheme::sButtonBorderSize(2, 2, 2, 2);
00054 nsMargin nsNativeTheme::sButtonDisabledBorderSize(1, 1, 1, 1);
00055 PRUint8  nsNativeTheme::sButtonActiveBorderStyle = NS_STYLE_BORDER_STYLE_INSET;
00056 PRUint8  nsNativeTheme::sButtonInactiveBorderStyle = NS_STYLE_BORDER_STYLE_OUTSET;
00057 nsILookAndFeel::nsColorID nsNativeTheme::sButtonBorderColorID = nsILookAndFeel::eColor_threedface;
00058 nsILookAndFeel::nsColorID nsNativeTheme::sButtonDisabledBorderColorID = nsILookAndFeel::eColor_threedshadow;
00059 nsILookAndFeel::nsColorID nsNativeTheme::sButtonBGColorID = nsILookAndFeel::eColor_threedface;
00060 nsILookAndFeel::nsColorID nsNativeTheme::sButtonDisabledBGColorID = nsILookAndFeel::eColor_threedface;
00061 nsMargin nsNativeTheme::sTextfieldBorderSize(2, 2, 2, 2);
00062 PRUint8  nsNativeTheme::sTextfieldBorderStyle = NS_STYLE_BORDER_STYLE_INSET;
00063 nsILookAndFeel::nsColorID nsNativeTheme::sTextfieldBorderColorID = nsILookAndFeel::eColor_threedface;
00064 PRBool   nsNativeTheme::sTextfieldBGTransparent = PR_FALSE;
00065 nsILookAndFeel::nsColorID nsNativeTheme::sTextfieldBGColorID = nsILookAndFeel::eColor__moz_field;
00066 nsILookAndFeel::nsColorID nsNativeTheme::sTextfieldDisabledBGColorID = nsILookAndFeel::eColor_threedface;
00067 nsMargin nsNativeTheme::sListboxBorderSize(2, 2, 2, 2);
00068 PRUint8  nsNativeTheme::sListboxBorderStyle = NS_STYLE_BORDER_STYLE_INSET;
00069 nsILookAndFeel::nsColorID nsNativeTheme::sListboxBorderColorID = nsILookAndFeel::eColor_threedface;
00070 PRBool   nsNativeTheme::sListboxBGTransparent = PR_FALSE;
00071 nsILookAndFeel::nsColorID nsNativeTheme::sListboxBGColorID = nsILookAndFeel::eColor__moz_field;
00072 nsILookAndFeel::nsColorID nsNativeTheme::sListboxDisabledBGColorID = nsILookAndFeel::eColor_threedface;
00073 
00074 nsNativeTheme::nsNativeTheme()
00075 {
00076   mDisabledAtom = do_GetAtom("disabled");
00077   mCheckedAtom = do_GetAtom("checked");
00078   mSelectedAtom = do_GetAtom("selected");
00079   mFocusedAtom = do_GetAtom("focused");
00080   mFirstTabAtom = do_GetAtom("first-tab");
00081   mDefaultAtom = do_GetAtom("default");
00082   mValueAtom = do_GetAtom("value");
00083   mModeAtom = do_GetAtom("mode");
00084   mClassAtom = do_GetAtom("class");
00085   mSortDirectionAtom = do_GetAtom("sortDirection");
00086   mReadOnlyAtom = do_GetAtom("readonly");
00087 }
00088 
00089 nsIPresShell *
00090 nsNativeTheme::GetPresShell(nsIFrame* aFrame)
00091 {
00092   if (!aFrame)
00093     return nsnull;
00094 
00095   // this is a workaround for the egcs 1.1.2 not inliningg
00096   // aFrame->GetPresContext(), which causes an undefined symbol
00097   nsPresContext *context = aFrame->GetStyleContext()->GetRuleNode()->GetPresContext();
00098   return context ? context->GetPresShell() : nsnull;
00099 }
00100 
00101 PRInt32
00102 nsNativeTheme::GetContentState(nsIFrame* aFrame, PRUint8 aWidgetType)
00103 {
00104   if (!aFrame)
00105     return 0;
00106 
00107   PRBool isXULCheckboxRadio = 
00108     (aWidgetType == NS_THEME_CHECKBOX || aWidgetType == NS_THEME_RADIO) &&
00109     aFrame->GetContent()->IsContentOfType(nsIContent::eXUL);
00110   if (isXULCheckboxRadio)
00111     aFrame = aFrame->GetParent();
00112 
00113   nsIPresShell *shell = GetPresShell(aFrame);
00114   if (!shell)
00115     return 0;
00116 
00117   PRInt32 flags = 0;
00118   shell->GetPresContext()->EventStateManager()->GetContentState(aFrame->GetContent(), flags);
00119   
00120   if (isXULCheckboxRadio && aWidgetType == NS_THEME_RADIO) {
00121     if (IsFocused(aFrame))
00122       flags |= NS_EVENT_STATE_FOCUS;
00123   }
00124   
00125   return flags;
00126 }
00127 
00128 PRBool
00129 nsNativeTheme::CheckBooleanAttr(nsIFrame* aFrame, nsIAtom* aAtom)
00130 {
00131   if (!aFrame)
00132     return PR_FALSE;
00133 
00134   nsIContent* content = aFrame->GetContent();
00135   if (content->IsContentOfType(nsIContent::eHTML))
00136     return content->HasAttr(kNameSpaceID_None, aAtom);
00137 
00138   nsAutoString attr;
00139   content->GetAttr(kNameSpaceID_None, aAtom, attr);
00140 
00141   // For XML/XUL elements, an attribute must be equal to the literal
00142   // string "true" to be counted as true.  An empty string should _not_
00143   // be counted as true.
00144 
00145   return attr.EqualsLiteral("true");
00146 }
00147 
00148 PRInt32
00149 nsNativeTheme::CheckIntAttr(nsIFrame* aFrame, nsIAtom* aAtom)
00150 {
00151   if (!aFrame)
00152     return 0;
00153 
00154   nsAutoString attr;
00155   aFrame->GetContent()->GetAttr(kNameSpaceID_None, aAtom, attr);
00156   PRInt32 err, value = attr.ToInteger(&err);
00157   if (NS_FAILED(err))
00158     return 0;
00159 
00160   return value;
00161 }
00162 
00163 PRBool
00164 nsNativeTheme::GetAttr(nsIFrame* aFrame, nsIAtom* aAtom, nsAString& attrValue)
00165 {
00166   if (!aFrame)
00167     return PR_FALSE;
00168 
00169   nsresult res = aFrame->GetContent()->GetAttr(kNameSpaceID_None, aAtom, attrValue);
00170   return ((res != NS_CONTENT_ATTR_NOT_THERE) &&
00171          !(res != NS_CONTENT_ATTR_NO_VALUE && attrValue.IsEmpty()));
00172 }
00173 
00174 PRBool
00175 nsNativeTheme::GetCheckedOrSelected(nsIFrame* aFrame, PRBool aCheckSelected)
00176 {
00177   if (!aFrame)
00178     return PR_FALSE;
00179 
00180   nsIContent* content = aFrame->GetContent();
00181 
00182   if (content->IsContentOfType(nsIContent::eXUL)) {
00183     // For a XUL checkbox or radio button, the state of the parent determines
00184     // the checked state
00185     aFrame = aFrame->GetParent();
00186   } else {
00187     // Check for an HTML input element
00188     nsCOMPtr<nsIDOMHTMLInputElement> inputElt = do_QueryInterface(content);
00189     if (inputElt) {
00190       PRBool checked;
00191       inputElt->GetChecked(&checked);
00192       return checked;
00193     }
00194   }
00195 
00196   return CheckBooleanAttr(aFrame, aCheckSelected ? mSelectedAtom : mCheckedAtom);
00197 }
00198 
00199 static void
00200 ConvertMarginToTwips(const nsMargin &aSource, nsMargin &aDest, float p2t)
00201 {
00202   aDest.top = NSIntPixelsToTwips(aSource.top, p2t);
00203   aDest.left = NSIntPixelsToTwips(aSource.left, p2t);
00204   aDest.bottom = NSIntPixelsToTwips(aSource.bottom, p2t);
00205   aDest.right = NSIntPixelsToTwips(aSource.right, p2t);
00206 }
00207 
00208 PRBool
00209 nsNativeTheme::IsWidgetStyled(nsPresContext* aPresContext, nsIFrame* aFrame,
00210                               PRUint8 aWidgetType)
00211 {
00212   // Check for specific widgets to see if HTML has overridden the style.
00213   if (aFrame && (aWidgetType == NS_THEME_BUTTON ||
00214                  aWidgetType == NS_THEME_TEXTFIELD ||
00215                  aWidgetType == NS_THEME_LISTBOX ||
00216                  aWidgetType == NS_THEME_DROPDOWN)) {
00217 
00218     if (aFrame->GetContent()->IsContentOfType(nsIContent::eHTML)) {
00219       nscolor defaultBGColor, defaultBorderColor;
00220       PRUint8 defaultBorderStyle;
00221       nsMargin defaultBorderSize;
00222       PRBool defaultBGTransparent = PR_FALSE;
00223 
00224       float p2t;
00225       p2t = aPresContext->PixelsToTwips();
00226 
00227       nsILookAndFeel *lookAndFeel = aPresContext->LookAndFeel();
00228 
00229       switch (aWidgetType) {
00230       case NS_THEME_BUTTON:
00231         if (IsDisabled(aFrame)) {
00232           ConvertMarginToTwips(sButtonDisabledBorderSize, defaultBorderSize, p2t);
00233           defaultBorderStyle = sButtonInactiveBorderStyle;
00234           lookAndFeel->GetColor(sButtonDisabledBorderColorID,
00235                                 defaultBorderColor);
00236           lookAndFeel->GetColor(sButtonDisabledBGColorID,
00237                                 defaultBGColor);
00238         } else {
00239           PRInt32 contentState = GetContentState(aFrame, aWidgetType);
00240           ConvertMarginToTwips(sButtonBorderSize, defaultBorderSize, p2t);
00241           if (contentState & NS_EVENT_STATE_HOVER &&
00242               contentState & NS_EVENT_STATE_ACTIVE)
00243             defaultBorderStyle = sButtonActiveBorderStyle;
00244           else
00245             defaultBorderStyle = sButtonInactiveBorderStyle;
00246           lookAndFeel->GetColor(sButtonBorderColorID,
00247                                 defaultBorderColor);
00248           lookAndFeel->GetColor(sButtonBGColorID,
00249                                 defaultBGColor);
00250         }
00251         break;
00252 
00253       case NS_THEME_TEXTFIELD:
00254         defaultBorderStyle = sTextfieldBorderStyle;
00255         ConvertMarginToTwips(sTextfieldBorderSize, defaultBorderSize, p2t);
00256         lookAndFeel->GetColor(sTextfieldBorderColorID,
00257                               defaultBorderColor);
00258         if (!(defaultBGTransparent = sTextfieldBGTransparent)) {
00259           if (IsDisabled(aFrame))
00260             lookAndFeel->GetColor(sTextfieldDisabledBGColorID,
00261                                   defaultBGColor);
00262           else
00263             lookAndFeel->GetColor(sTextfieldBGColorID,
00264                                   defaultBGColor);
00265         }
00266         break;
00267 
00268       case NS_THEME_LISTBOX:
00269       case NS_THEME_DROPDOWN:
00270         defaultBorderStyle = sListboxBorderStyle;
00271         ConvertMarginToTwips(sListboxBorderSize, defaultBorderSize, p2t);
00272         lookAndFeel->GetColor(sListboxBorderColorID,
00273                               defaultBorderColor);
00274         if (!(defaultBGTransparent = sListboxBGTransparent)) {
00275           if (IsDisabled(aFrame))
00276             lookAndFeel->GetColor(sListboxDisabledBGColorID,
00277                                   defaultBGColor);
00278           else
00279             lookAndFeel->GetColor(sListboxBGColorID,
00280                                   defaultBGColor);
00281         }
00282         break;
00283 
00284       default:
00285         NS_ERROR("nsNativeTheme::IsWidgetStyled widget type not handled");
00286         return PR_FALSE;
00287       }
00288 
00289       // Check whether background differs from default
00290       const nsStyleBackground* ourBG = aFrame->GetStyleBackground();
00291 
00292       if (defaultBGTransparent) {
00293         if (!(ourBG->mBackgroundFlags & NS_STYLE_BG_COLOR_TRANSPARENT))
00294           return PR_TRUE;
00295       } else if (ourBG->mBackgroundColor != defaultBGColor ||
00296                  ourBG->mBackgroundFlags & NS_STYLE_BG_COLOR_TRANSPARENT)
00297         return PR_TRUE;
00298 
00299       if (!(ourBG->mBackgroundFlags & NS_STYLE_BG_IMAGE_NONE))
00300         return PR_TRUE;
00301 
00302       // Check whether border style or color differs from default
00303       const nsStyleBorder* ourBorder = aFrame->GetStyleBorder();
00304 
00305       for (PRInt32 i = 0; i < 4; ++i) {
00306         if (ourBorder->GetBorderStyle(i) != defaultBorderStyle)
00307           return PR_TRUE;
00308 
00309         PRBool borderFG, borderClear;
00310         nscolor borderColor;
00311         ourBorder->GetBorderColor(i, borderColor, borderFG, borderClear);
00312         if (borderColor != defaultBorderColor || borderClear)
00313           return PR_TRUE;
00314       }
00315 
00316       // Check whether border size differs from default
00317       if (ourBorder->GetBorder() != defaultBorderSize)
00318         return PR_TRUE;
00319     }
00320   }
00321 
00322   return PR_FALSE;
00323 }