1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2002
20 * the Initial Developer. All Rights Reserved.
23 * Brian Ryner <bryner@brianryner.com> (Original Author)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsNativeTheme.h"
40 #include "nsIDocument.h"
41 #include "nsIContent.h"
43 #include "nsIPresShell.h"
44 #include "nsPresContext.h"
45 #include "nsIEventStateManager.h"
47 #include "nsINameSpaceManager.h"
48 #include "nsIDOMHTMLInputElement.h"
49 #include "nsILookAndFeel.h"
50 #include "nsThemeConstants.h"
51 #include "nsIComponentManager.h"
53 nsNativeTheme::nsNativeTheme()
58 nsNativeTheme::GetPresShell(nsIFrame
* aFrame
)
63 // this is a workaround for the egcs 1.1.2 not inliningg
64 // aFrame->GetPresContext(), which causes an undefined symbol
65 nsPresContext
*context
= aFrame
->GetStyleContext()->GetRuleNode()->GetPresContext();
66 return context
? context
->GetPresShell() : nsnull
;
70 nsNativeTheme::GetContentState(nsIFrame
* aFrame
, PRUint8 aWidgetType
)
75 PRBool isXULCheckboxRadio
=
76 (aWidgetType
== NS_THEME_CHECKBOX
||
77 aWidgetType
== NS_THEME_RADIO
) &&
78 aFrame
->GetContent()->IsNodeOfType(nsINode::eXUL
);
79 if (isXULCheckboxRadio
)
80 aFrame
= aFrame
->GetParent();
82 nsIPresShell
*shell
= GetPresShell(aFrame
);
87 shell
->GetPresContext()->EventStateManager()->GetContentState(aFrame
->GetContent(), flags
);
89 if (isXULCheckboxRadio
&& aWidgetType
== NS_THEME_RADIO
) {
90 if (IsFocused(aFrame
))
91 flags
|= NS_EVENT_STATE_FOCUS
;
98 nsNativeTheme::CheckBooleanAttr(nsIFrame
* aFrame
, nsIAtom
* aAtom
)
103 nsIContent
* content
= aFrame
->GetContent();
104 if (content
->IsNodeOfType(nsINode::eHTML
))
105 return content
->HasAttr(kNameSpaceID_None
, aAtom
);
107 // For XML/XUL elements, an attribute must be equal to the literal
108 // string "true" to be counted as true. An empty string should _not_
109 // be counted as true.
110 return content
->AttrValueIs(kNameSpaceID_None
, aAtom
,
111 NS_LITERAL_STRING("true"), eCaseMatters
);
115 nsNativeTheme::CheckIntAttr(nsIFrame
* aFrame
, nsIAtom
* aAtom
, PRInt32 defaultValue
)
121 aFrame
->GetContent()->GetAttr(kNameSpaceID_None
, aAtom
, attr
);
122 PRInt32 err
, value
= attr
.ToInteger(&err
);
123 if (attr
.IsEmpty() || NS_FAILED(err
))
130 nsNativeTheme::GetCheckedOrSelected(nsIFrame
* aFrame
, PRBool aCheckSelected
)
135 nsIContent
* content
= aFrame
->GetContent();
137 if (content
->IsNodeOfType(nsINode::eXUL
)) {
138 // For a XUL checkbox or radio button, the state of the parent determines
140 aFrame
= aFrame
->GetParent();
142 // Check for an HTML input element
143 nsCOMPtr
<nsIDOMHTMLInputElement
> inputElt
= do_QueryInterface(content
);
146 inputElt
->GetChecked(&checked
);
151 return CheckBooleanAttr(aFrame
, aCheckSelected
? nsWidgetAtoms::selected
152 : nsWidgetAtoms::checked
);
156 nsNativeTheme::IsWidgetStyled(nsPresContext
* aPresContext
, nsIFrame
* aFrame
,
159 // Check for specific widgets to see if HTML has overridden the style.
161 (aWidgetType
== NS_THEME_BUTTON
||
162 aWidgetType
== NS_THEME_TEXTFIELD
||
163 aWidgetType
== NS_THEME_TEXTFIELD_MULTILINE
||
164 aWidgetType
== NS_THEME_LISTBOX
||
165 aWidgetType
== NS_THEME_DROPDOWN
) &&
166 aFrame
->GetContent()->IsNodeOfType(nsINode::eHTML
) &&
167 aPresContext
->HasAuthorSpecifiedRules(aFrame
,
168 NS_AUTHOR_SPECIFIED_BORDER
|
169 NS_AUTHOR_SPECIFIED_BACKGROUND
);
173 nsNativeTheme::IsFrameRTL(nsIFrame
* aFrame
)
175 return aFrame
&& aFrame
->GetStyleVisibility()->mDirection
== NS_STYLE_DIRECTION_RTL
;
180 nsNativeTheme::GetScrollbarButtonType(nsIFrame
* aFrame
)
185 static nsIContent::AttrValuesArray strings
[] =
186 {&nsWidgetAtoms::scrollbarDownBottom
, &nsWidgetAtoms::scrollbarDownTop
,
187 &nsWidgetAtoms::scrollbarUpBottom
, &nsWidgetAtoms::scrollbarUpTop
,
190 switch (aFrame
->GetContent()->FindAttrValueIn(kNameSpaceID_None
,
191 nsWidgetAtoms::sbattr
,
192 strings
, eCaseMatters
)) {
193 case 0: return eScrollbarButton_Down
| eScrollbarButton_Bottom
;
194 case 1: return eScrollbarButton_Down
;
195 case 2: return eScrollbarButton_Bottom
;
196 case 3: return eScrollbarButton_UpTop
;
203 nsNativeTheme::TreeSortDirection
204 nsNativeTheme::GetTreeSortDirection(nsIFrame
* aFrame
)
207 return eTreeSortDirection_Natural
;
209 static nsIContent::AttrValuesArray strings
[] =
210 {&nsWidgetAtoms::descending
, &nsWidgetAtoms::ascending
, nsnull
};
211 switch (aFrame
->GetContent()->FindAttrValueIn(kNameSpaceID_None
,
212 nsWidgetAtoms::sortdirection
,
213 strings
, eCaseMatters
)) {
214 case 0: return eTreeSortDirection_Descending
;
215 case 1: return eTreeSortDirection_Ascending
;
218 return eTreeSortDirection_Natural
;
222 nsNativeTheme::IsLastTreeHeaderCell(nsIFrame
* aFrame
)
227 // A tree column picker is always the last header cell.
228 if (aFrame
->GetContent()->Tag() == nsWidgetAtoms::treecolpicker
)
231 // Find the parent tree.
232 nsIContent
* parent
= aFrame
->GetContent()->GetParent();
233 while (parent
&& parent
->Tag() != nsWidgetAtoms::tree
) {
234 parent
= parent
->GetParent();
237 // If the column picker is visible, this can't be the last column.
238 if (parent
&& !parent
->AttrValueIs(kNameSpaceID_None
, nsWidgetAtoms::hidecolumnpicker
,
239 NS_LITERAL_STRING("true"), eCaseMatters
))
242 while ((aFrame
= aFrame
->GetNextSibling())) {
243 if (aFrame
->GetRect().width
> 0)
251 nsNativeTheme::IsBottomTab(nsIFrame
* aFrame
)
256 nsAutoString classStr
;
257 aFrame
->GetContent()->GetAttr(kNameSpaceID_None
, nsWidgetAtoms::_class
, classStr
);
258 return !classStr
.IsEmpty() && classStr
.Find("tab-bottom") != kNotFound
;
262 nsNativeTheme::IsFirstTab(nsIFrame
* aFrame
)
267 return aFrame
->GetContent()->HasAttr(kNameSpaceID_None
, nsWidgetAtoms::firsttab
);
271 nsNativeTheme::IsLastTab(nsIFrame
* aFrame
)
276 return aFrame
->GetContent()->HasAttr(kNameSpaceID_None
, nsWidgetAtoms::lasttab
);
280 nsNativeTheme::IsHorizontal(nsIFrame
* aFrame
)
285 return !aFrame
->GetContent()->AttrValueIs(kNameSpaceID_None
, nsWidgetAtoms::orient
,
286 nsWidgetAtoms::vertical
,
291 nsNativeTheme::IsNextToSelectedTab(nsIFrame
* aFrame
, PRInt32 aOffset
)
297 return IsSelectedTab(aFrame
);
299 PRInt32 thisTabIndex
= -1, selectedTabIndex
= -1;
301 nsIFrame
* currentTab
= aFrame
->GetParent()->GetFirstChild(NULL
);
302 for (PRInt32 i
= 0; currentTab
; currentTab
= currentTab
->GetNextSibling()) {
303 if (currentTab
->GetRect().width
== 0)
305 if (aFrame
== currentTab
)
307 if (IsSelectedTab(currentTab
))
308 selectedTabIndex
= i
;
312 if (thisTabIndex
== -1 || selectedTabIndex
== -1)
315 return (thisTabIndex
- selectedTabIndex
== aOffset
);
320 nsNativeTheme::IsIndeterminateProgress(nsIFrame
* aFrame
)
325 return aFrame
->GetContent()->AttrValueIs(kNameSpaceID_None
, nsWidgetAtoms::mode
,
326 NS_LITERAL_STRING("undetermined"),